Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Server Side Programming Articles
Page 248 of 2109
Program to find how many updates required to make string half monotonous in Python
Suppose we have a lowercase string s whose length is even. We have to find the minimum number of characters that need to be updated such that one of the following three conditions is satisfied for all i, where 0 ≤ i < n/2 and j, n/2 ≤ j < n − s[i] > s[j] (left half characters greater than right half) s[i] < s[j] (left half characters less than right half) s[i] == s[j] (left half characters equal to right half) So, if the input is ...
Read MorePython Pandas - How to Round the DateTimeIndex with minute frequency
To round the DateTimeIndex with minute frequency, use the DateTimeIndex.round() method. For minute frequency, use the freq parameter with value 'T'. Creating a DateTimeIndex First, let's create a DateTimeIndex with seconds frequency to demonstrate the rounding operation ? import pandas as pd # DatetimeIndex with period 5 and frequency as 45 seconds # timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, tz='Australia/Adelaide', freq='45s') # display DateTimeIndex print("DateTimeIndex...", datetimeindex) print("DateTimeIndex frequency...", datetimeindex.freq) The output of the above code is ? DateTimeIndex... DatetimeIndex(['2021-09-29 07:00:00+09:30', '2021-09-29 07:00:45+09:30', ...
Read MorePython Pandas - How to Round the DateTimeIndex with hourly frequency
To round the DateTimeIndex with hourly frequency, use the DateTimeIndex.round() method. For hourly frequency, use the freq parameter with value 'H'. Creating a DateTimeIndex At first, import the required libraries and create a DateTimeIndex with period 5 and frequency as 35 minutes − import pandas as pd # Create DatetimeIndex with period 5 and frequency as 35 minutes datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, tz='Australia/Adelaide', freq='35T') # Display DateTimeIndex print("DateTimeIndex...", datetimeindex) DateTimeIndex... DatetimeIndex(['2021-09-29 07:00:00+09:30', '2021-09-29 07:35:00+09:30', '2021-09-29 ...
Read MoreProgram to find minimum costs needed to fill fruits in optimized way in Python
Suppose we have a list called fruits and two values k and cap. Each fruits[i] contains three values: [c, s, t], where fruit i costs c each, has size s, and there are t total fruits available. The k represents number of fruit baskets with capacity cap. We want to fill the fruit baskets with the following constraints in this order ? Each basket can only hold same type fruits Each basket should be as full as possible Each basket should be as cheap as possible ...
Read MorePython Pandas - Snap time stamps in DateTimeIndex to nearest occurring frequency
To snap time stamps in DateTimeIndex to nearest occurring frequency, use the DateTimeIndex.snap() method. This method rounds timestamps to the nearest frequency boundary, such as month-end, week-start, or any other valid frequency. Syntax DateTimeIndex.snap(freq) Parameters freq: A frequency string (e.g., 'M' for month-end, 'W' for week, 'D' for day) Basic Example Let's create a DateTimeIndex and snap timestamps to the nearest month-end ? import pandas as pd # Create DatetimeIndex with period 6 and frequency as D i.e. day # The timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-10-20 ...
Read MoreProgram to find minimum cost to send same number of people to two different cities in Python
Suppose we have a list called costs, where costs[i] has [c1, c2] indicating that for person i it costs c1 amount to reach city 0 and c2 amount to reach city 1. We want the same number of people to go to city 0 as city 1, and we need to find the minimum cost required. So, if the input is like costs = [[2, 6], [10, 3], [4, 9], [5, 8]], then the output will be 17, because person 0 and 2 will go to city 0 and person 1 and 3 to city 1. For city 0, ...
Read MoreProgram to check first player can win by reaching total sum to target in Python
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 MoreProgram to find indices or local peaks in Python
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 MorePython Pandas - Return an Index of formatted strings specified by date format
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 MorePython Pandas - Convert times to midnight in DateTimeIndex
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