Program to check first player can win by reaching total sum to target in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:15:49

309 Views

Suppose we have two numbers k and target. Now consider Amal and Bimal are playing a game. In each round, Amal picks a number from 1 to k to add to the total score that initially starts from 0. Whoever crosses the total to target wins. Amal always plays first, we have to check whether he can force a win if both of them play optimally. So, if the input is like k = 5, target = 10, then the output will be True, as if Amal picks 4 first, then whether Bimal picks 1, 2, ..., or 5, ... Read More

Program to find indices or local peaks in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:15:34

326 Views

A local peak is an element that is greater than or equal to its neighbors. In Python, we can find indices of local peaks by comparing each element with its adjacent elements. A peak can be a single element or a plateau (consecutive equal elements that are peaks). Peak Definition An index i is a peak when these conditions are met: The next different number is either absent or smaller than nums[i] The previous different number is either absent or smaller than nums[i] There is at least one different number on either side Algorithm ... Read More

Python Pandas - Return an Index of formatted strings specified by date format

AmitDiwan
Updated on 26-Mar-2026 17:15:12

348 Views

The DateTimeIndex.strftime() method in Pandas returns an Index of formatted strings based on a specified date format. This method is useful for converting datetime objects into human-readable string representations. Syntax DateTimeIndex.strftime(date_format) Parameters: date_format − A string specifying the format using strftime directives Creating a DateTimeIndex First, let's create a DateTimeIndex with timezone information − import pandas as pd # Create DatetimeIndex with period 7 and frequency as 2 days datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='2D') print("DateTimeIndex...") print(datetimeindex) DateTimeIndex... DatetimeIndex(['2021-10-30 02:30:50+10:30', '2021-11-01 02:30:50+10:30', ... Read More

Python Pandas - Convert times to midnight in DateTimeIndex

AmitDiwan
Updated on 26-Mar-2026 17:14:51

1K+ Views

To convert times to midnight in DateTimeIndex, use the DateTimeIndex.normalize() method in Pandas. This method sets the time component of all datetime values to 00:00:00 while preserving the date and timezone information. What is normalize()? The normalize() method converts the time component of datetime values to midnight (00:00:00). This is useful when you want to work with dates only, ignoring the time portion. Creating a DateTimeIndex First, let's create a DateTimeIndex with various times ? import pandas as pd # Create DateTimeIndex with period 7 and frequency as 10H (10 hours) # The ... Read More

Python Pandas - Return index locations of values between particular time of day including start time in DateTimeIndex

AmitDiwan
Updated on 26-Mar-2026 17:14:30

149 Views

To return index locations of values between particular time of day in DateTimeIndex, use the DateTimeIndex.indexer_between_time() method. Set the include_start parameter to True for including the start time. Syntax DateTimeIndex.indexer_between_time(start_time, end_time, include_start=True, include_end=True) Parameters Parameter Description start_time Start time as string in format 'HH:MM:SS' end_time End time as string in format 'HH:MM:SS' include_start Whether to include start time (default: True) include_end Whether to include end time (default: True) Example Let's create a DatetimeIndex and find index locations between specific times ... Read More

Program to find sum of the minimums of each sublist from a list in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:14:12

375 Views

Suppose we have a list of numbers called nums. We have to find the sum of minimum values for every possible sublist in nums. If the answer is too large, then mod the result by 10^9 + 7. So, if the input is like nums = [5, 10, 20, 10, 0], then the output will be 90 because the sublists are [[5], [10], [20], [10], [0], [5, 10], [10, 20], [20, 10], [10, 0], [5, 10, 20], [10, 20, 10], [20, 10, 0], [5, 10, 20, 10], [10, 20, 10, 0], [5, 10, 20, 10, 0]], and their minimum ... Read More

Python Pandas - Return index locations of values between particular time of day in DateTimeIndex

AmitDiwan
Updated on 26-Mar-2026 17:13:48

311 Views

To return index locations of values between particular time of day in DateTimeIndex, use the DateTimeIndex.indexer_between_time() method. This method is useful for filtering time-series data based on specific time ranges. Syntax DateTimeIndex.indexer_between_time(start_time, end_time, include_start=True, include_end=True) Parameters The method accepts the following parameters: start_time − The start time in 'HH:MM:SS' format end_time − The end time in 'HH:MM:SS' format include_start − Boolean to include start time (default: True) include_end − Boolean to include end time (default: True) Creating a DateTimeIndex First, let's create a DateTimeIndex with timezone information − ... Read More

Python Pandas - Return index locations of values at particular time of day in DateTimeIndex

AmitDiwan
Updated on 26-Mar-2026 17:13:28

670 Views

To return index locations of values at particular time of day in DateTimeIndex, use the DateTimeIndex.indexer_at_time() method. This method returns an array of integer positions where the time component matches the specified time. Syntax DateTimeIndex.indexer_at_time(time, asof=False) Parameters The key parameters are ? time ? Time as a time object or string asof ? Return the latest index location if exact time not found (default: False) Creating DateTimeIndex First, let's create a DateTimeIndex with timezone-aware timestamps ? import pandas as pd # Create DatetimeIndex with 20-minute intervals ... Read More

Program to check every sublist in a list containing at least one unique element in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:13:04

416 Views

Suppose we have a list of elements called nums, we have to check whether every sublist has at least 1 element in it that occurs exactly once in the sublist or not. We have to solve this problem in linear time. So, if the input is like nums = [5, 10, 20, 10, 0], then the output will be True, because every sublist in nums has at least one element which has occurred only once. [[5], [10], [20], [10], [0], [5, 10], [10, 20], [20, 10], [10, 0], [5, 10, 20], [10, 20, 10], [20, 10, 0], [5, 10, ... Read More

Python Pandas - Detect the frequency of the given DatetimeIndex object

AmitDiwan
Updated on 26-Mar-2026 17:12:46

265 Views

To detect the frequency of a given DatetimeIndex object, use the DatetimeIndex.inferred_freq property. This property automatically infers the frequency pattern from the datetime values. Creating a DatetimeIndex First, let's create a DatetimeIndex with a specific frequency ? import pandas as pd # Create DatetimeIndex with 5 periods and 3-year frequency # Using Australia/Adelaide timezone datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=5, tz='Australia/Adelaide', freq='3Y') print("DateTimeIndex...") print(datetimeindex) DateTimeIndex... DatetimeIndex(['2021-12-31 02:30:50+10:30', '2024-12-31 02:30:50+10:30', '2027-12-31 02:30:50+10:30', '2030-12-31 02:30:50+10:30', ... Read More

Advertisements