Found 10476 Articles for Python

Program to find out if a linked list is present in a given binary tree in Python

Arnab Chakraborty
Updated on 19-Oct-2021 12:48:04

213 Views

Suppose we are given a binary tree that has a root node 'root' and a linked list that has a head node 'head'. We have to find out if that linked list exists in that binary tree. If a set of nodes in the tree have links with each other in order as a linked list, and if that order is similar to that of the provided linked list, then we return 'True' or otherwise, we return 'False'.So, if the input is likeTreeLinked Listthen the output will be True.To solve this, we will follow these steps −arr := a new ... Read More

Python Pandas - Return TimeDeltaIndex as object ndarray of datetime.datetime objects

AmitDiwan
Updated on 19-Oct-2021 11:07:31

123 Views

To return TimeDeltaIndex as object ndarray of datetime.datetime objects, use the TimeDeltaIndex.to_pytimedelta() 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'])Return TimeDeltaIndex as object ndarray −print("Return TimeDeltaIndex as object ndarray of datetime.datetime objects...", tdIndex.to_pytimedelta())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

Python Pandas - Return a dataframe of the components of the TimedeltaIndex

AmitDiwan
Updated on 19-Oct-2021 11:02:14

144 Views

To return a dataframe of the components of the TimedeltaIndex, use the TimedeltaIndex.components 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 35s 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. The components include days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds −print("The Dataframe of the components of TimeDeltas...", tdIndex.components)ExampleFollowing is the code −import pandas as pd # Create a TimeDeltaIndex object # We have ... Read More

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

AmitDiwan
Updated on 19-Oct-2021 10:59:50

114 Views

To extract the number of microseconds for each element from TimeDeltaIndex object, use the TimedeltaIndex.nanoseconds 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 35s 3us 10ns', '+22:39:19.999999', '2 day 4h 03:08:02.000045', '+21:15:45.999999'])Display the number of nanoseconds from each element of TimeDeltaIndex −print("The number of nanoseconds from the TimeDeltaIndex object...", tdIndex.nanoseconds)ExampleFollowing is the code −import pandas as pd # Create a TimeDeltaIndex object # We have set the timedelta-like data using the 'data' parameter as ... Read More

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

AmitDiwan
Updated on 19-Oct-2021 10:57:39

111 Views

To extract the number of microseconds for each element from TimeDeltaIndex object, use the TimedeltaIndex.microseconds 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 35s 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 microseconds from each element of TimeDeltaIndex −print("The number of microseconds from the TimeDeltaIndex object...", tdIndex.microseconds)ExampleFollowing is the code −import pandas as pd # Create a TimeDeltaIndex object # We have set the timedelta-like data using ... Read More

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

AmitDiwan
Updated on 19-Oct-2021 10:54:01

166 Views

To extract the Number of seconds for each element from TimeDeltaIndex object, use the TimedeltaIndex.seconds propertyAt 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 35s 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 seconds from each element of TimeDeltaIndex −print("The number of seconds from the TimeDeltaIndex object...", tdIndex.seconds)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 find length of longest repeating substring in a string in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:46:49

3K+ Views

Suppose we have a lowercase string s, we have to find the length of the longest substring that occurs at least twice in s. If we cannot find such string, return 0.So, if the input is like s = "abdgoalputabdtypeabd", then the output will be 3, because the longest substring that occurs more than once is "abd".To solve this, we will follow these steps −Define a function lcs() . This will take s1, s2n := minimum of size of s1 and size of s2for i in range 0 to n - 1, doif s1[i] is not same as s2[i], thenreturn ... Read More

Program to find length longest prefix sequence of a word array in Python

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

235 Views

Suppose we have a list of words called w, with lowercase strings. We have to find the length of the longest sequence of w where each previous word is the prefix of the next word and the next word has just one new character appended.So, if the input is like w = ["pqr", "pq", "m", "mn", "pqrs"], then the output will be 3 because we can get the sequence: ["pq", "pqr", "pqrs"], whose length is 3.To solve this, we will follow these steps −sort the list wdp := a map, where default value for a key is 0res := 0for ... Read More

Program to find length of longest matrix path length in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:40:59

286 Views

Suppose we have a binary matrix, where 0 indicates empty cell and 1 indicates wall. We can start at any empty cell on first row and want to end up on any empty cell on the last row. We can move left, right, or down, we have to find the longest such path where we can visit each cell at most once. If this is not possible, then return 0.So, if the input is like000000010000then the output will be 10, as We can move (0, 3), (0, 2), (0, 1), (0, 0), (1, 0), (1, 1), (1, 2), (2, 2), ... Read More

Program to find length of longest contiguously strictly increasing sublist after removal in Python

Arnab Chakraborty
Updated on 19-Oct-2021 10:37:33

528 Views

Suppose we have a list of numbers called nums, we have to find the maximum length of a contiguous strictly increasing sublist. We are allowed to remove at most single element from the list.So, if the input is like nums = [35, 5, 6, 7, 8, 9, 12, 11, 26], then the output will be 7, because if we remove 12 from nums, the list will be [5, 6, 7, 8, 9, 11, 26], the length is 7, this is the longest, contiguous, strictly increasing sub-list.To solve this, we will follow these steps −if nums is empty, thenreturn 0end := ... Read More

Advertisements