Found 10784 Articles for Python

Program to find first positive missing integer in range in Python

Arnab Chakraborty
Updated on 08-Oct-2020 10:28:27

290 Views

Suppose we have a list of sorted list of distinct integers of size n, we have to find the first positive number in range [1 to n+1] that is not present in the array.So, if the input is like nums = [0, 5, 1], then the output will be 2, as 2 is the first missing number in range 1 to 5.To solve this, we will follow these steps −target := 1for each i in arr, doif i is same as target, thentarget := target + 1return targetLet us see the following implementation to get better understanding −Example Live Democlass Solution: ... Read More

Program to find lowest possible integer that is missing in the array in Python

Arnab Chakraborty
Updated on 08-Oct-2020 10:25:57

128 Views

Suppose we have a list of numbers called nums, we have to find the first missing positive number. In other words, the lowest positive number that does not present in the array. The array can contain duplicates and negative numbers as well.So, if the input is like nums = [0, 3, 1], then the output will be 2To solve this, we will follow these steps −nums := a set with all positive numbers present in numsif nums is null, thenreturn 1for i in range 1 to size of nums + 2, doif i is not present in nums, thenreturn iLet ... Read More

Program to find minimum amount needed to be paid all good performers in Python

Arnab Chakraborty
Updated on 08-Oct-2020 10:20:11

288 Views

Suppose we have given a list of numbers called ratings, and this is showing the performance scores of coders. Now the manager wants to give Rs 1000 to every coder except if two coders are adjacent, they would like to pay the better performing coder at least Rs 1000 higher than the worse performing one. We have to find the minimum amount the manager can pay following above constraints.So, if the input is like ratings = [1, 2, 5, 1], then the output will be 7000, as the minimum we can pay for each respective coder is [1000, 2000, 3000, ... Read More

Program to check a number can be written as a sum of distinct factorial numbers or not in Python

Arnab Chakraborty
Updated on 08-Oct-2020 10:17:12

160 Views

Suppose we have a positive number n, we have to check whether n can be written as the sum of unique positive factorial numbers or not.So, if the input is like n = 144, then the output will be True, as 4! + 5! = 24 + 120 = 144To solve this, we will follow these steps −fact := 1res := a new listx := 2while fact = res[i], thenn := n - res[i]return true when n is same as 0Let us see the following implementation to get better understanding −Example Live Democlass Solution: def solve(self, n):    fact = 1 ... Read More

Program to find the sum of the absolute differences of every pair in a sorted list in Python

Arnab Chakraborty
Updated on 07-Oct-2020 14:31:59

160 Views

Suppose we have a list of sorted numbers called nums, we have to find the sum of the absolute differences between every pair of numbers in the given list. Here we will consider (i, j) and (j, i) are different pairs. If the answer is very large, mod the result by 10^9+7.So, if the input is like nums = [2, 4, 8], then the output will be 24, as |2 - 4| + |2 - 8| + |4 - 2| + |4 - 8| + |8 - 2| + |8 - 4|.To solve this, we will follow these steps −m ... Read More

Program to check robot can reach target position or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:47:26

331 Views

Suppose we have a robot, that is currently sitting in at position (0, 0) (Cartesian plane). If we have list of its moves that it can make, containing N(North), S(South), W(West), and E(East). We have to check whether it can reach at destination coordinate (x, y).So, if the input is like moves = ['N', 'N', 'E', 'E', 'S'], (x, y) = (2, 1), then the output will be True, To solve this, we will follow these steps −temp_coord := [0, 0]for each move in moves, doif move is same as "N", thentemp_coord[1] := temp_coord[1] + 1otherwise when move is same ... Read More

Program to check we can reach leftmost or rightmost position or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:44:57

293 Views

Suppose we have a string containing letters of three types, R, B, and dot(.). Here R stands for our current position, B stands for a blocked position, and dot(.) stands for an empty position. Now, in one step, we can move to any adjacent position to our current position, as long as it is valid (empty). We have to check whether we can reach either the leftmost position or the rightmost position.So, if the input is like s = "...........R.....BBBB.....", then the output will be True, as R can reach left most position, because there is no block.To solve this, ... Read More

Program to find how many years it will take to reach t amount in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:43:46

146 Views

Suppose we have some parameters P, O, E, T. If we have P dollars in principal that we want to invest the stock market. The stock market alternates between first returning E and then O percent interest per year, we have to check how many years it would take to reach at least T dollars.So, if the input is like P = 200, O = 10, E = 25, T = 300, then the output will be 3 as in the first year we will get interest 25%, so end up with 200+50 = 250, then next year we will ... Read More

Program to reverse the position of each word of a given string in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:42:28

434 Views

Suppose we have a string of words delimited by spaces; we have to reverse the order of words.So, if the input is like "Hello world, I love python programming", then the output will be "programming python love I world, Hello"To solve this, we will follow these steps −temp := make a list of words by splitting s using blank spacetemp := reverse the list tempreturn a string by joining the elements from temp using space delimiter.Let us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s):       temp = s.split(' ')   ... Read More

Program to check if we reverse sublist of one list to form second list or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:41:16

148 Views

Suppose we have two lists of numbers called A, and B. We have to take some sublist in A and reverse it. Then check whether it is possible to turn A into B or not. We can take sublist and reverse it any number of times.So, if the input is like A = [2, 3, 4, 9, 10], B = [4, 3, 2, 10, 9], then the output will be True as we can reverse [2, 3, 4] and [9, 10].To solve this, we will follow these steps −res := a map, initially emptyfor each n in nums, dores[n] := ... Read More

Advertisements