Found 26504 Articles for Server Side Programming

Program to find how many updates required to make string half monotonous in Python

Arnab Chakraborty
Updated on 18-Oct-2021 13:15:56

162 Views

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]s[i] < s[j]s[i] == s[j]So, if the input is like s = "pppxxp", then the output will be 1 because if we change the last "p" to "x", then this can satisfy the condition s[i] < s[j]To solve this, we will follow these steps −n := size ... Read More

Python Pandas - How to Round the DateTimeIndex with minute frequency

AmitDiwan
Updated on 18-Oct-2021 13:14:16

1K+ Views

To round the DateTimeIndex with minute frequency, use the DateTimeIndex.round() method. For minute frequency, use the freq parameter with value ‘T’.At first, import the required libraries −import pandas as pdDatetimeIndex with period 5 and frequency as s i.e. seconds. The 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)Round operation on DateTimeIndex date with minute frequency. For minute frequency, we have used 'T −print("Performing round operation with minute frequency...", datetimeindex.round(freq='T'))ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 5 and frequency as s i.e. seconds # timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-09-29 ... Read More

Python Pandas - How to Round the DateTimeIndex with hourly frequency

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

889 Views

To round the DateTimeIndex with hourly frequency, use the DateTimeIndex.round() method. For hourly frequency, use the freq parameter with value ‘H’.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 5 and frequency as T i.e. minutes −datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, tz='Australia/Adelaide', freq='35T') Display DateTimeIndex −print("DateTimeIndex...", datetimeindex)Round operation on DateTimeIndex date with hourly frequency. For hourly frequency, we have used 'H' −print("Performing round operation with hourly frequency...", datetimeindex.round(freq='H'))ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 5 and frequency as T i.e. minutes # timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, ... Read More

Program to find minimum costs needed to fill fruits in optimized way in Python

Arnab Chakraborty
Updated on 18-Oct-2021 13:13:11

2K+ Views

Suppose we have a list called fruits and another two values k and cap. Where each fruits[i] has three values: [c, s, t], this indicates fruit i costs c each, size of each of them is s, and there is total t of them. The k represents number of fruit baskets of capacity cap. We want to fill the fruit baskets with the following constraints in this order −Each basket can only hold same type fruitsEach basket should be as full as possibleEach basket should be as cheap as possibleSo we have to find the minimum cost required to fill ... Read More

Python Pandas - Snap time stamps in DateTimeIndex to nearest occurring frequency

AmitDiwan
Updated on 18-Oct-2021 13:09:06

227 Views

To snap time stamps in DateTimeIndex to nearest occurring frequency, use the DateTimeIndex.snap() method. Set the frequency using the freq parameter.At 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/Adelaide', freq='D') Display DateTimeIndex −print("DateTimeIndex...", datetimeindex)Snap time stamps to nearest occurring i.e. Month end here −print("Snap time stamps to nearest occurring frequency...", datetimeindex.snap(freq='M'))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') ... Read More

Program to find minimum cost to send same number of people to two different cities in Python

Arnab Chakraborty
Updated on 18-Oct-2021 13:10:29

174 Views

Suppose we have a list called costs. Where costs[i] has [c1, c2] indicates that for person i it costs c1 amount to reach city 0 and costs c2 amount to reach city 1. We want the same number of people to go to city 0 as city 1, we have 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, so for city 0, the ... Read More

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

Arnab Chakraborty
Updated on 18-Oct-2021 13:08:29

218 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 plays 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, Amal can ... Read More

Program to find indices or local peaks in Python

Arnab Chakraborty
Updated on 18-Oct-2021 13:06:24

247 Views

Suppose we have a list of numbers called nums. We have to find the index of every peak element in the nums, sorted in ascending order. An index i of a peak element when all of these three conditions are satisfied: 1. The next number on its right that's different than nums[i] doesn't present or must be smaller than nums[i] 2. The previous number on its left that's different than nums[i] doesn't present or must be smaller than nums[i] 3. There is at least one number on its left side or right side that is different from nums[i].So, if the ... Read More

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

AmitDiwan
Updated on 18-Oct-2021 13:05:24

292 Views

To return an Index of formatted strings specified by date format, use the DateTimeIndex.strftime() method in Pandas.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 7 and frequency as D i.e. days −datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='2D') Display DateTimeIndex −print("DateTimeIndex...", datetimeindex)Formatted −print("Format with different directives...", datetimeindex.strftime('%b. %d, %Y was a %A'))ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 7 and frequency as D i.e. days # The timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='2D') # display DateTimeIndex print("DateTimeIndex...", datetimeindex) # display DateTimeIndex frequency ... Read More

Python Pandas - Convert times to midnight in DateTimeIndex

AmitDiwan
Updated on 18-Oct-2021 13:02:50

1K+ Views

To convert times to midnight in DateTimeIndex, use the DateTimeIndex.normalize() in Pandas.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 7 and frequency as H i.e. hours −datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='10H') Display DateTimeIndex −print("DateTimeIndex...", datetimeindex)The time component of the date-time is converted to midnight i.e. 00:00:00 −print("Normalize (converted the time component to midnight)...", datetimeindex.normalize())ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 7 and frequency as H i.e. hours # The timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='10H') # display DateTimeIndex print("DateTimeIndex...", datetimeindex) ... Read More

Advertisements