Found 26504 Articles for Server Side Programming

Program to count substrings with all 1s in binary string in Python

Arnab Chakraborty
Updated on 18-Oct-2021 12:03:47

558 Views

Suppose we have a binary string s. We have to find the number of substrings that contain only "1"s. If the answer is too large, mod the result by 10^9+7.So, if the input is like s = "100111", then the output will be 7, because the substrings containing only "1"s are ["1", "1", "1", "1", "11", "11" and "111"]To solve this, we will follow these steps −a := 0count := 0for i in range 0 to size of s - 1, doif s[i] is same as "0", thena := 0otherwise, a := a + 1count := count + areturn countExampleLet ... Read More

Program to count number of square submatrices in given binary matrix in Python

Arnab Chakraborty
Updated on 18-Oct-2021 12:01:36

468 Views

Suppose we have a 2D binary matrix. We have to find the total number of square submatrices present in matrix, where all elements are 1.So, if the input is like011011then the output will be 5, because there is one (2 × 2) square, and four (1 × 1) squaresTo solve this, we will follow these steps −if mat is empty, thenreturn 0c := 0for i in range 0 to row count of mat, dofor j in range 0 to column count of mat, doif mat[i, j] is 1, thenif i is 0 or j is 0, thenc := c + ... Read More

Program to count number of intervals that is totally contained inside other intervals in Python

Arnab Chakraborty
Updated on 18-Oct-2021 11:56:48

550 Views

Suppose we have a list of intervals. In this list interval[i] has [start, end] values. We have to find the number of intervals are contained by another interval. If there is an interval that is contained by multiple other intervals that should only be counted once. An interval [s0, e0] is inside another interval [s1, e1] when s0 ≤ s1 and e0 ≥ e1.So, if the input is like intervals = [[2, 6], [3, 4], [4, 7], [5, 5]], then the output will be 2, because [3, 4] and [5, 5] are inside [2, 6] and [4, 7] respectively.To solve ... Read More

Python Pandas - Extract the hour from the DateTimeIndex with specific time series frequency

AmitDiwan
Updated on 18-Oct-2021 11:06:44

4K+ Views

To extract hour from the DateTimeIndex with specific time series frequency, use the DateTimeIndex.hour property.At first, import the required libraries −import pandas as pdDatetimeIndex with period 6 and frequency as H i.e. hour. The timezone is Australia/Sydney −datetimeindex = pd.date_range('2021-10-20 02:35:55', periods=6, tz='Australia/Sydney', freq='H') Display DateTimeIndex −print("DateTimeIndex...", datetimeindex)Get the hour −print("Getting the hour..", datetimeindex.hour) ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 6 and frequency as H i.e. hour # The timezone is Australia/Sydney datetimeindex = pd.date_range('2021-10-20 02:35:55', periods=6, tz='Australia/Sydney', freq='H') # display DateTimeIndex print("DateTimeIndex...", datetimeindex) # display DateTimeIndex frequency print("DateTimeIndex frequency...", datetimeindex.freq) ... Read More

Program to find all contiguously increasing numbers in start end range in Python

Arnab Chakraborty
Updated on 18-Oct-2021 11:54:03

426 Views

Suppose we have two numbers start and end, we have to find a sorted list of integers such that every number e in range [start, end] both inclusive and the digits of e are contiguously increasing. An example of continuously increasing number is 5678, but 169 is not.So, if the input is like start = 10 end = 150, then the output will be [12, 23, 34, 45, 56, 67, 78, 89, 123]To solve this, we will follow these steps −s := all 9 digits as a string "123456789"a := a new listfor i in range 0 to 8, dofor j in range i + 1 to 9, dox := (substring of s from index i to j-1) as numberif start

Program to count number of ways to win at most k consecutive games in Python

Arnab Chakraborty
Updated on 18-Oct-2021 10:55:26

318 Views

Suppose we have two numbers n and k. Here n represents the number of games we are going to play. We have to find in how many ways we can win k or fewer games consecutively. If the answer is too large then mod the result by 10^9 + 7.So, if the input is like n = 3 k = 2, then the output will be 7, as the possible ways in which we can win 2 or fewer times consecutively, are ["LLL", "WLL", "LWL", "LLW", "WWL", "LWW", "WLW"]To solve this, we will follow these steps −m := 1^9 + ... Read More

Check elementwise if the Intervals in the IntervalIndex contain the value in Python Pandas

AmitDiwan
Updated on 18-Oct-2021 08:15:57

191 Views

To return an IntervalArray identical to the current one but closed on the specified side, use the set_closed() method with parameter set as both.At first, import the required libraries −import pandas as pdCreate IntervalArray −index = pd.arrays.IntervalArray.from_breaks(range(6)) Display the interval −print("IntervalIndex...", index)Return an IntervalArray identical to the current one but closed on specified side i.e. "both" here −print("Result...", index.set_closed('both')) ExampleFollowing is the code −import pandas as pd # Create IntervalArray index = pd.arrays.IntervalArray.from_breaks(range(6)) # Display the interval print("IntervalIndex...", index) # Display the interval length print("IntervalIndex length...", index.length) # the left bound print("The left bound for the ... Read More

Python Pandas - Return an IntervalArray identical to the current one but closed on the left side

AmitDiwan
Updated on 18-Oct-2021 08:07:24

129 Views

To return an IntervalArray identical to the current one but closed on the left side, use the set_closed() method with value left.At first, import the required libraries −import pandas as pdCreate IntervalArray −index = pd.arrays.IntervalArray.from_breaks(range(5))Display the interval −print("IntervalIndex...", index)Return an IntervalArray identical to the current one but closed on specified side i.e. "left" here −print("Result...", index.set_closed('left')) ExampleFollowing is the code −import pandas as pd # Create IntervalArray index = pd.arrays.IntervalArray.from_breaks(range(5)) # Display the interval print("IntervalIndex...", index) # Display the interval length print("IntervalIndex length...", index.length) # the left bound print("The left bound for the IntervalIndex...", index.left) ... Read More

Python Pandas IntervalIndex - Get the locations of all the relevant interval if a label is in several intervals

AmitDiwan
Updated on 18-Oct-2021 07:59:43

197 Views

To get the locations of all the relevant interval if a label is in several intervals, use the get_loc() method in Pandas.At first, import the required libraries −import pandas as pdCreate two Interval objects. Closed intervals set using the "closed" parameter with value "both"interval1 = pd.Interval(50, 75) interval2 = pd.Interval(75, 90) interval3 = pd.Interval(50, 90)Create IntervalIndex from the three intervals −index = pd.IntervalIndex([interval1, interval2, interval3]) Get the locations of all the relevant interval if a label is in several intervals −print("Get the locations of all the relevant interval...", index.get_loc(65))ExampleFollowing is the code −import pandas as pd # Create two ... Read More

Python Pandas IntervalIndex - Get integer location for requested label

AmitDiwan
Updated on 18-Oct-2021 07:57:32

200 Views

To get integer location for requested label, use the get_loc() method in Pandas. At first, import the required libraries −import pandas as pdCreate two Interval objects. Closed intervals set using the "closed" parameter with value "both" −interval1 = pd.Interval(50, 75) interval2 = pd.Interval(75, 90)Create IntervalIndex from the two intervals −index = pd.IntervalIndex([interval1, interval2]) Get integer location for requested label −print("Integer location for requested label...", index.get_loc(75))ExampleFollowing is the code −import pandas as pd # Create two Interval objects # Closed intervals set using the "closed" parameter with value "both" interval1 = pd.Interval(50, 75) interval2 = pd.Interval(75, 90) # display ... Read More

Advertisements