Programming Articles

Page 246 of 2547

Program to find largest size to truncate logs to store them completely in database in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 166 Views

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. The limit represents the total size of logs we can store in our database. We need to find the largest x such that if we truncate every log in logs to be at most size x, the sum of the truncated 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, ...

Read More

Program to find minimum length of first split of an array with smaller elements than other list in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 234 Views

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 part2. 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]. Algorithm Approach To solve this problem, we follow these steps ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 480 Views

To extract the number of days for each element from a TimedeltaIndex object, use the TimedeltaIndex.days property. This property returns an Int64Index containing only the days component from each timedelta. Creating a TimedeltaIndex First, import pandas and create a TimedeltaIndex with various time duration formats − import pandas as pd # Create a TimeDeltaIndex object with different time formats tdIndex = pd.TimedeltaIndex(data=['10 day 5h 2 min 3us 10ns', '+22:39:19.999999', ...

Read More

Program to count number of on lights flipped by n people in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 265 Views

Suppose we have n toggle switches in a room and n people present in that room. They flip switches according to a specific pattern: Person 1 comes and flips all switches (1, 2, 3, 4, ...) Person 2 comes and flips switches that are multiples of 2 (2, 4, 6, 8, ...) Person i comes and flips switches that are multiples of i We need to find how many switches will be in the ON position after all n people have finished flipping. Understanding the Problem ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 440 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
AmitDiwan
Updated on 26-Mar-2026 963 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
AmitDiwan
Updated on 26-Mar-2026 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
AmitDiwan
Updated on 26-Mar-2026 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
Arnab Chakraborty
Updated on 26-Mar-2026 505 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
AmitDiwan
Updated on 26-Mar-2026 527 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
Showing 2451–2460 of 25,466 articles
« Prev 1 244 245 246 247 248 2547 Next »
Advertisements