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
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
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
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
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
Suppose we have two numbers n and k. We have to find the lexicographically smallest lowercase string of size k and distance n. The distance is the sum of the letter number in alphabet. For example, 'a' has letter number 1, 'b' has 2, 'y' has 25, 'z' has 26 as so on.So, if the input is like n = 15 k = 3, then the output will be "aam", because "aam" is the lexicographically smallest string of length 3 whose distance is 1 + 1 + 13 = 15.To solve this, we will follow these steps −dist := an ... Read More
Suppose we have an edge list where each items are holding (u, v) represents u is parent of v. We have to find the length of the longest path in the tree. The path length is 1 + number of nodes in that path.So, if the input is likethen the output will be 5, because the path is [1, 4, 5, 7], there are 4 nodes in total, so path length is 1 + 4 = 5.To solve this, we will follow these steps −g := adjacency list of the graph from given edge listd := a new mapDefine a ... Read More
To create a DataFrame from DateTimeIndex, use the datetimeindex.to_frame(). We have set the name parameter to override the name of the resulting column.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 5 and frequency as S i.e. seconds −datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S')Display DateTimeIndex −print("DateTimeIndex...", datetimeindex) The original index isn't set in the returned DataFrame using the 'False' parameter. To override the name of the resulting column, we have used the 'name' parameter −print("DateTimeIndex to DataFrame...", datetimeindex.to_frame(index=False, name = 'DateTimeData'))ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 5 ... Read More
To create a DataFrame from DateTimeIndex ignoring the index, use the DateTimeIndex.to_frame() method. Set the parameter index to False to ignore the index.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 5 and frequency as S i.e. seconds −datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S')Display DateTimeIndex −print("DateTimeIndex...", datetimeindex) Create a DataFrame from DateTimeIndex. The original index isn't set in the returned DataFrame using the 'False' parameter −print("DateTimeIndex to DataFrame...", datetimeindex.to_frame(index=False))ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 5 and frequency as S i.e. seconds # timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-10-18 ... Read More
To convert the DateTimeIndex to Series excluding the TimeZone, use the datetimeindex.tz_convert(None).to_series(). The tz.convert(None) is used to exclude the timezone.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 5 and frequency as S i.e. seconds −datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S')Display DateTimeIndex −print("DateTimeIndex...", datetimeindex) Convert DateTimeIndex to Series. Use the "tz_convert()" and set it to "None" to exclude the TimeZone −print("DateTimeIndex to series excluding the TimeZone...", datetimeindex.tz_convert(None).to_series())ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 5 and frequency as S i.e. seconds # timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP