Found 10476 Articles for Python

Program to find length of longest consecutively increasing substring in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:34:44

228 Views

Suppose we have a lowercase string s. This contains English letters as well as "?" Symbol. For each "?" we must either remove it or replace it with any lowercase letter. We have to find the length of the longest consecutively increasing substring that starts with letter "a".So, if the input is like s = "vta???defke", then the output will be 6, as we can turn s into "vtabcdefke" and "abcdef" is the longest consecutively increasing substring, and this is also starting with "a".To solve this, we will follow these steps −maxlen := 0length := 0qmarks := 0for each c ... Read More

Program to find length of longest consecutive sublist with unique elements in Python

Arnab Chakraborty
Updated on 19-Oct-2021 13:43:10

350 Views

Suppose we have a list of numbers called nums, where all elements are unique. We have to find the length of the longest sublist that contains consecutive elements.So, if the input is like nums = [3, 6, 7, 5, 4, 9], then the output will be 5, because the sublist is [3, 6, 7, 5, 4] this contains all consecutive elements from 3 to 7.To solve this, we will follow these steps −ret := 0for i in range 0 to size of nums - 1, dolhs := nums[i]rhs := nums[i]for j in range i to size of nums - 1, ... Read More

Program to find longest consecutive run of 1s in binary form of n in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:29:27

730 Views

Suppose we have a non-negative value n, we have to find the length of the longest consecutive run of 1s in its binary representation.So, if the input is like n = 1469, then the output will be 4, because binary representation of 156 is "10110111101", so there are four consecutive 1sTo solve this, we will follow these steps −count := 0while n is not same as 0, don := n AND (n after shifting one bit to the left)count := count + 1return countExampleLet us see the following implementation to get better understanding −def solve(n):    count = 0    while n != 0:       n = n & (n

Program to find length of longest arithmetic subsequence with constant difference in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:26:39

216 Views

Suppose we have a list of numbers nums and another value diff, we have to find the length of the longest arithmetic subsequence where the difference between any consecutive numbers in the subsequence is same as diff.So, if the input is like nums = [-1, 1, 4, 7, 2, 10] diff = 3, then the output will be 4, because, we can pick the subsequence like [1, 4, 7, 10].To solve this, we will follow these steps −seen := an empty dictionary, default value is 0 when key is not presentmx := 0for each x in nums, doif x - ... Read More

Program to find length of longest substring with 1s in a binary string after one 0-flip in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:24:25

657 Views

Suppose we have a binary string s. We are allowed to flip at most one "0" to "1", we have to find the length of the longest contiguous substring of 1s.So, if the input is like s = "1010110001", then the output will be 4, as if we flip the zero present at index 3, then we get the string "1011110001", here length of the longest substring of 1s is 4.To solve this, we will follow these steps −n := size of sans := 0, ones := 0, left := 0, right := 0while right < n, doif s[right] is ... Read More

Program to find largest size to truncate logs to store them completely in database in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:21:36

127 Views

Suppose we have a list of numbers called logs and another value limit. Each element in logs[i] represents the size of logs generated by the i-th user. And limit represents the total size of logs we can store in our database. We have to find the largest x such that if we truncate every log in logs to be at most size x, and the sum of the left log sizes is at most limit. If no log needs to be truncated, then simply return the largest log size.So, if the input is like logs = [500, 200, 10000, 500, ... Read More

Program to find minimum length of first split of an array with smaller elements than other list in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:19:28

189 Views

Suppose we have a list of numbers nums, we want to split the list into two parts part1 and part2 such that every element in part1 is less than or equal to every element in part1. We have to find the smallest length of part1 that is possible (not 0 length).So, if the input is like nums = [3, 1, 2, 5, 4], then the output will be 3, because we can split the list like part1 = [3, 1, 2] and part2 = [5, 4].To solve this, we will follow these steps −p := minimum of numss := 0for ... Read More

Python Pandas - Extract the Number of days for each element from TimeDeltaIndex

AmitDiwan
Updated on 19-Oct-2021 10:22:29

423 Views

To extract the Number of days for each element from TimeDeltaIndex object, use the TimedeltaIndex.days property.At first, import the required libraries −import pandas as pdCreate a TimeDeltaIndex object. We have set the timedelta-like data using the 'data' parameter as well −tdIndex = pd.TimedeltaIndex(data =['10 day 5h 2 min 3us 10ns', '+22:39:19.999999', '2 day 4h 03:08:02.000045', '+21:15:45.999999'])Display TimedeltaIndex −print("TimedeltaIndex...", tdIndex) Display the number of days from each element of TimeDeltaIndex −print("The number of days from the TimeDeltaIndex object...", tdIndex.days)ExampleFollowing is the code −import pandas as pd # Create a TimeDeltaIndex object # We have set the timedelta-like data using the ... Read More

Program to count number of on lights flipped by n people in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:17:19

197 Views

Suppose we have a number n, consider there are n toggle switches in a room and there are n people present in that room, they flip switches as follows −Person 1 comes and flips all switches.Person 2 comes and flips switches that are multiples of 2: 2, 4, 6, ...Person i comes and flips switches that are multiples of i. and so on.We have to find the number of switches that will be in on position finally.So, if the input is like n = 5, then the output will be 2, as initially bulbs are [0, 0, 0, 0, 0].After ... Read More

Python Pandas - Create a TimeDeltaIndex object

AmitDiwan
Updated on 19-Oct-2021 10:24:30

726 Views

To create a TimeDeltaIndex object, use the pandas.TimedeltaIndex() method.At first, import the required libraries −import pandas as pdCreate a TimeDeltaIndex object. We have set the timedelta-like data using the 'data' parameter as well −tdIndex = pd.TimedeltaIndex(data =['10 day 5h 2 min 3us 10ns', '+22:39:19.999999', '2 day 4h 03:08:02.000045', '+21:15:45.999999'])Display TimedeltaIndex −print("TimedeltaIndex...", tdIndex)Return a dataframe of the components of TimeDeltas −print("The Dataframe of the components of TimeDeltas...", tdIndex.components)ExampleFollowing is the code −import pandas as pd # Create a TimeDeltaIndex object # We have set the timedelta-like data using the 'data' parameter as well tdIndex = pd.TimedeltaIndex(data =['10 day 5h 2 min ... Read More

Advertisements