Find Number of Islands We Cannot Leave in Python

Arnab Chakraborty
Updated on 18-Oct-2021 12:42:17

193 Views

Suppose we have a binary matrix. Here 1 represents land and 0 represents water. From any land we can move up, down, left or right but not diagonally to another land cell or go off the matrix. We have to find the number of land cells from which we cannot go off the matrix.So, if the input is like0001011001100001then the output will be 4, as There's 4 land squares in the middle from which we cannot walk off the matrix.To solve this, we will follow these steps −q := a list of pairs (i, j) for each row i and ... Read More

Extract Frequency from DatetimeIndex in Python Pandas

AmitDiwan
Updated on 18-Oct-2021 12:40:15

2K+ Views

To extract the frequency from the DateTimeIndex, use the DateTimeIndex.freq property in Pandas.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 6 and frequency as D i.e. day. The timezone is Australia/Adelaide −datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Adelaide', freq='D') Display DateTimeIndex −print("DateTimeIndex...", datetimeindex)Display DateTimeIndex frequency −print("DateTimeIndex frequency...", datetimeindex.freq) ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 6 and frequency as D i.e. day # The timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Adelaide', freq='D') # display DateTimeIndex print("DateTimeIndex...", datetimeindex) # display DateTimeIndex frequency print("DateTimeIndex frequency...", datetimeindex.freq)OutputThis will ... Read More

Extract Timezone from DatetimeIndex with Specific Frequency in Python Pandas

AmitDiwan
Updated on 18-Oct-2021 12:38:02

1K+ Views

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

Find Stone Removal Rate in K Hours Using Python

Arnab Chakraborty
Updated on 18-Oct-2021 12:37:15

128 Views

Suppose we have a list of numbers called piles and a value k. The piles[i] represents, the number of stones on the pile i. On each hour, we select any pile and remove r number of stones from that pile. If we pick a pile with fewer than r stones, it still takes an hour to clear the pile. We have to find the minimum value of r, such that we can remove all the stones in less than or equal to k hours.So, if the input is like piles = [3, 6, 4] k = 5, then the output ... Read More

Extract Quarter from DatetimeIndex in Python Pandas

AmitDiwan
Updated on 18-Oct-2021 12:36:12

2K+ Views

To extract the quarter of the date from the DateTimeIndex with specific time series frequency, use the DateTimeIndex.quarter.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 6 and frequency as M i.e. Month. The timezone is Australia/Sydney −datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='2M') Display DateTimeIndex frequency −print("DateTimeIndex frequency...", datetimeindex.freq)Get the quarter of the date −print("Get the quarter of the date..", datetimeindex.quarter) Result is based on the following quarters of an year −Quarter 1 = 1st January to 31st March Quarter 2 = 1st April to 30th June Quarter 3 = 1st July to 30th ... Read More

Extract Day of Week from DatetimeIndex in Python Pandas

AmitDiwan
Updated on 18-Oct-2021 12:34:47

309 Views

To extract the day of week from the DateTimeIndex with specific time series frequency, use the DateTimeIndex.dayofweek propertyAt first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 6 and frequency as D i.e. day −datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='3D') Display DateTimeIndex frequency −print("DateTimeIndex frequency...", datetimeindex.freq)Get the day of week. It is assumed the week starts on Monday, which is denoted by 0 and ends on Sunday which is denoted by 6 −print("Get the day of week..", datetimeindex.dayofweek) ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 6 and frequency as ... Read More

Find Dropped Correct Sensor Value from Faulty List in Python

Arnab Chakraborty
Updated on 18-Oct-2021 12:34:20

140 Views

Suppose we have two lists nums1 and nums2, they are representing sensor metrics. Each list contains unique values, so a ≠ b. One of these two lists are holding accurate sensor metrics but the other one contains faulty. In the faulty list one value, that is not the last value was dropped and a wrong value was placed to the end of that list. We have to find the actual value that was dropped.So, if the input is like nums1 = [5, 10, 15] nums2 = [10, 15, 8], then the output will be 5, as first list nums1 holds ... Read More

Extract Ordinal Day of Year from DatetimeIndex in Python Pandas

AmitDiwan
Updated on 18-Oct-2021 12:33:20

345 Views

To extract the ordinal day of year from the DateTimeIndex with specific time series frequency, use the DateTimeIndex.dayofyear propertyAt first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 6 and frequency as D i.e. day −datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='D') Display DateTimeIndex −print("DateTimeIndex...", datetimeindex)Get the day of year −print("Get the ordinal day of year..", datetimeindex.dayofyear) ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 6 and frequency as D i.e. day # The timezone is Australia/Sydney datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Sydney', freq='D') # display DateTimeIndex print("DateTimeIndex...", datetimeindex) ... Read More

Count Ways Ball Can Drop to Lowest Level by Avoiding Blacklisted Steps in Python

Arnab Chakraborty
Updated on 18-Oct-2021 12:32:08

258 Views

Suppose we have a value h and a list of numbers called blacklist. We are currently at height h, and are playing a game to move a small ball down to height 0. Now, in even rounds (starting from 0) we can move the ball 1, 2, or 4 stairs down. And in odd rounds, we can move the ball 1, 3, or 4 stairs down. Some levels are blacklisted. So if the ball reach there, it will die immediately. We have to find the number of ways the ball can move down at height 0. If the answer is ... Read More

Return Numpy Array of Python Datetime Objects with Timezone Information

AmitDiwan
Updated on 18-Oct-2021 12:31:45

260 Views

To return numpy array of python datetime.time objects with timezone information, use the datetimeindex.timetz property.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 5 and frequency as us i.e. nanoseconds −datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=5, tz='Australia/Sydney', freq='ns') Display DateTimeIndex −print("DateTimeIndex...", datetimeindex)Returns only the time part of Timestamp with timezone information −print("The numpy array (time part with timezone)..", datetimeindex.timetz) ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 5 and frequency as us i.e. nanoseconds # The timezone is Australia/Sydney datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=5, tz='Australia/Sydney', freq='ns') # display DateTimeIndex print("DateTimeIndex...", ... Read More

Advertisements