Program to find lexicographically smallest lowercase string of length k and distance n in Python

Arnab Chakraborty
Updated on 26-Mar-2026 17:35:53

471 Views

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 and 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. Algorithm To solve this, we will ... Read More

Python Pandas - Create a DataFrame from DateTimeIndex but override the name of the resulting column

AmitDiwan
Updated on 26-Mar-2026 17:35:32

982 Views

To create a DataFrame from a DateTimeIndex, use the datetimeindex.to_frame() method. The name parameter allows you to override the column name in the resulting DataFrame. Creating a DateTimeIndex First, let's create a DateTimeIndex with timezone information ? import pandas as pd # Create a DateTimeIndex with period 5, frequency 40 seconds, and timezone datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, tz='Australia/Adelaide', freq='40S') print("DateTimeIndex...") print(datetimeindex) ... Read More

Python Pandas - Create a DataFrame from DateTimeIndex ignoring the index

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

3K+ Views

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 and create a regular DataFrame column instead. Syntax DateTimeIndex.to_frame(index=True, name=None) Parameters index − Boolean value. If True (default), uses DateTimeIndex as DataFrame index. If False, creates a regular column. name − Column name for the DataFrame. If None, uses the DateTimeIndex name or defaults to 0. Creating DateTimeIndex First, let's create a DateTimeIndex with timezone and frequency ? import pandas as pd # Create DatetimeIndex ... Read More

Python Pandas - Convert the DateTimeIndex to Series

AmitDiwan
Updated on 26-Mar-2026 17:34:49

2K+ Views

To convert a DateTimeIndex to Series, use the DateTimeIndex.to_series() method. This method creates a Series where both the index and values are the same datetime objects from the original DateTimeIndex. Syntax DateTimeIndex.to_series(index=None, name=None) Parameters index − Index to use for the Series (optional). If None, uses the DateTimeIndex itself. name − Name for the resulting Series (optional). Creating a DateTimeIndex First, let's create a DateTimeIndex with 5 periods and 40-second frequency in Australia/Adelaide timezone ? import pandas as pd # Create DatetimeIndex with period 5 and frequency as 40 ... Read More

Program to find length of the longest path in an n-ary tree in Python

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

520 Views

In this problem, we need to find the longest path in an n-ary tree represented by an edge list. Each edge (u, v) indicates that u is the parent of v. The path length is calculated as 1 + number of nodes in the path. For the given tree structure: 1 ... Read More

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

AmitDiwan
Updated on 26-Mar-2026 17:34:05

536 Views

In Pandas, a DatetimeIndex stores datetime values as NumPy datetime64 objects. Sometimes you need to convert these to native Python datetime.datetime objects. Use the to_pydatetime() method to return a DatetimeIndex as an object ndarray of datetime.datetime objects. Syntax DatetimeIndex.to_pydatetime() This method returns an object ndarray where each element is a Python datetime.datetime object. Creating a DatetimeIndex First, let's create a DatetimeIndex with a specific frequency ? import pandas as pd # Create DatetimeIndex with period 5 and frequency as 2 years datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, freq='2Y') print("DateTimeIndex...") print(datetimeindex) ... Read More

Python Pandas - Calculate TimedeltaArray of difference between index values and index converted to PeriodArray at specified freq

AmitDiwan
Updated on 26-Mar-2026 17:33:43

282 Views

To calculate TimedeltaArray of difference between index values and index converted to PeriodArray at specified frequency, use the to_perioddelta() method on a DatetimeIndex. Set the frequency using the freq parameter. What is to_perioddelta()? The to_perioddelta() method calculates the time difference between each datetime value and the start of its corresponding period. For example, with monthly frequency ('M'), it shows how far into each month each datetime falls. Creating a DatetimeIndex First, create a DatetimeIndex with specific periods and frequency ? import pandas as pd # Create DatetimeIndex with 5 periods, every 2 years ... Read More

Python Pandas - How to convert DateTimeIndex to Period

AmitDiwan
Updated on 26-Mar-2026 17:33:20

2K+ Views

To convert DateTimeIndex to Period, use the datetimeindex.to_period() method in Pandas. The frequency is set using the freq parameter. Creating a DateTimeIndex First, let's create a DateTimeIndex with specific period and frequency ? import pandas as pd # Create DatetimeIndex with period 5 and frequency as 2 years datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5, freq='2Y') print("DateTimeIndex...") print(datetimeindex) print("DateTimeIndex frequency...") print(datetimeindex.freq) DateTimeIndex... DatetimeIndex(['2021-12-31 07:20:32.261811624', '2023-12-31 07:20:32.261811624', ... Read More

Program to find area of largest submatrix by column rearrangements in Python

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

334 Views

Suppose we have a binary matrix. We can rearrange the columns as many times as we want, then find the area of the largest submatrix containing only 1s. So, if the input is like ? 1 0 0 ... Read More

Python Pandas - How to perform ceil operation on the DateTimeIndex with specified frequency

AmitDiwan
Updated on 26-Mar-2026 17:32:37

156 Views

To perform ceil operation on the DateTimeIndex with specified frequency, use the DateTimeIndex.ceil() method. The freq parameter specifies the frequency to which each timestamp should be rounded up. What is Ceil Operation? The ceil() method rounds timestamps up to the nearest specified frequency unit. For example, if you ceil to microseconds ('us'), any nanosecond precision will be rounded up to the next microsecond. Syntax DateTimeIndex.ceil(freq) Parameters freq: String representing the frequency to ceil to (e.g., 'S' for seconds, 'us' for microseconds, 'H' for hours) Example Let's create a ... Read More

Advertisements