Found 10476 Articles for Python

Program to find maximum number of courses we can take based on interval time in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:24:08

263 Views

Suppose we have a list of intervals in the form [start, end], this is representing the start and end time of a course. We have to find the maximum number of courses we can take, assuming we can only take one course at a time and the start of a course needs to be later than the end of the last course.So, if the input is like times = [[3, 6], [6, 9], [7, 8], [9, 11]], then the output will be 3, as we can take courses [[3, 6], [7, 8], [9, 11]]To solve this, we will follow these ... Read More

Program to find number of boxes that form longest chain in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:21:39

226 Views

Suppose we have a list of boxes, here each entry has two values [start, end] (start < end). We can join two boxes if the end of one is equal to the start of another. We have to find the length of the longest chain of boxes.So, if the input is like blocks = [ [4, 5], [5, 6], [4, 8], [1, 2], [2, 4] ], then the output will be 4, as we can form the chain: [1, 2], [2, 4], [4, 5], [5, 6]To solve this, we will follow these steps:if boxes are empty, thenreturn 0sort the list ... Read More

Program to check whether first player win in candy remove game or not in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:17:11

198 Views

Suppose we have a list of numbers called candies and someone is playing a game against his/her friend. In each round, a player can remove any two consecutive candies with the same value. And whoever can not pick up a candy loses and that player1 start first, we have to check whether player1 will win or not.So, if the input is like nums = [2, 2, 5], then the output will be True, as if player1 picks the 2s then the other player cannot pick any candies.To solve this, we will follow these steps:stack := a new stackturns := 0for ... Read More

Program to evaluate Boolean expression from a string in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:15:08

908 Views

Suppose we have a string s containing a boolean expression with operators "and" and "or", evaluate it and return the result. Here the expressions may have parentheses, which should be evaluated first.So, if the input is like s = "T and (F or T)", then the output will be TrueTo solve this, we will follow these steps:stack := a new listt = list of elements of s split by blank spacesfor each v in t, doif v[0] is same as "(", thenpush true when v[from index of "(" to end] is same as "T", into stackotherwise when ")" is found, ... Read More

Program to find minimum bus fare for travelling all days in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:10:37

964 Views

Suppose we have a list of sorted numbers called days, where we must take the bus for on each day. We have to find the lowest cost it takes to travel for all the days. There are 3 types of bus tickets. 1-day pass for 2 bucks 7-day pass for 7 bucks 30-day pass for 25 bucksSo, if the input is like days = [1, 3, 5, 6, 28], then the output will be 9, as the lowest cost can be achieved by purchasing a 7-day pass in the beginning and then a 1-day pass on the 29th day.To solve ... Read More

Program to count number of swaps required to change one list to another in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:07:36

418 Views

Suppose we have two lists of numbers L1 and L2, the length of each list is n and each value is unique to its list, and values are in range 1 to n, we have to find the minimum number of adjacent swaps required to transform L1 to L2.So, if the input is like L1 = [0, 1, 2, 3] L2 = [2, 0, 1, 3], then the output will be 2, as we can swap 1 and 2, L1 will be [0, 2, 1, 3], and then 0 and 2, L1 will be [2, 0, 1, 3], this is ... Read More

Program to find maximum profit we can get by buying and selling stocks with a fee in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:05:39

189 Views

Suppose we have a list of stock prices of a company in chronological sequence, and also have the transaction fee for one sell transaction. We have to find the maximum profit we could have made from buying and selling that stock any number of times. We must buy before we can sell it.So, if the input is like prices = [2, 10, 4, 8] fee = 3, then the output will be 6, as we can buy at 2 and sell at 10 and incur a fee of 3, so profit is 5. Then we buy at 4 and sell ... Read More

Program to find number of places are safe when bomb explodes in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 07:58:13

183 Views

Suppose we have a 2d binary matrix, where 1 represents a bomb and 0 represents an empty cell. When a bomb explodes, all the spaces along on the same row and column are damaged. We have to find the number of spaces we can stand in to not get damaged.So, if the input is like110000000then the output will be 2, as there are two spaces the bottom right cell and the middle right cell are safe.To solve this, we will follow these steps:r := a list of size same as row count of matrix and fill with falsec := a ... Read More

Program to find minimum possible sum by changing 0s to 1s k times from a list of numbers in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 07:55:14

128 Views

Suppose we have a list of numbers called nums and another value k. We have to following operation k times: Select any number on the list. In the binary representation of that number, select a bit that is 0 and make it 1. Finally, we have to return the minimum possible sum of all the numbers after performing k operations. If the answer is too high, return result mode 10^9+7.So, if the input is like nums = [4, 7, 3] k = 2, then the output will be 17, as the binary representation of 4 is 100, 3 is 011, ... Read More

Program to find starting index of the child who receives last balloon in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 07:50:58

91 Views

Suppose we have n children standing in a circle, and they are waiting to get a balloon. The distribution is carried out starting with the kth child (first at index 0), and giving them a balloon they left the circle. Now every kth child gets a balloon going clockwise until there is only one child left that gets a balloon. So if we have n and k, we have to find the starting index of the child that receives the last balloon.So, if the input is like n = 3 k = 2, then the output will be 1, in ... Read More

Advertisements