Found 10476 Articles for Python

Program to check whether first player can take more candies than other or not in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:43:07

168 Views

Suppose we have a list of numbers called candies and two persons are racing to collect the most number of candies. Here the race is turn based, person 1 is starting first, and in each turn he can pick up candies from the front or from the back. We have to check whether person 1 can collect more candies than other or not.So, if the input is like candies = [1, 4, 3, 8], then the output will be True, as person 1 can take 8 candies in the first round and regardless of whether second person picks 1 or ... Read More

Program to count number of permutations where sum of adjacent pairs are perfect square in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:40:54

228 Views

Suppose we have a list of numbers called nums. We have to find the number of permutations of nums such that sum of every pair of adjacent values is a perfect square. Two permutations A and B are unique when there is some index i where A[i] is not same as B[i].So, if the input is like nums = [2, 9, 7], then the output will be 2, as we have [2, 7, 9] and [9, 7, 2]To solve this, we will follow these steps −res := 0Define a function util() . This will take iif i + 1 is ... Read More

Program to check whether final string can be formed using other two strings or not in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:38:50

152 Views

Suppose we have two strings s, t, and another string r we have to check whether there is any way to get r by merging characters in order from s and t.So, if the input is like s = "xyz" t = "mno" r = "xymnoz", then the output will be True, as xymnoz can be formed by interleaving xyz and mno.To solve this, we will follow these steps −Define a function solve() . This will take s, t, rif s, t and r are empty, thenreturn Trueif r is empty, thenreturn Falseif s is empty, thenreturn true when t ... Read More

Program to count number of flipping required to make all x before y in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:37:17

181 Views

Suppose we have a lowercase string s with letters x and y. Now consider an operation in which we change a single x to y or vice versa. We have to find the minimum number of times we need to perform that operation to set all x's come before all y's.So, if the input is like s = "yxyyyyxyxx", then the output will be 4.To solve this, we will follow these steps −y_left := 0x_right := number of "x" in s, res := number of "x" in sfor each item in s, doif item is same as "x", thenx_right := ... Read More

Program to check person can reach top-left or bottomright cell avoiding fire or not in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:34:04

130 Views

Suppose we have a 2D matrix with few different values like below −0 for empty cell1 for a person2 for fire3 for a wallNow assume there is only one person and in each turn the fire expands in all four directions (up, down, left and right) but fire cannot expand through walls. We have to check whether the person can move to either the top-left corner or the bottom-right corner or the matrix. We have to keep in mind that in each turn, the person moves first, then the fire expands. If the person makes it to any of the ... Read More

Program to count number of walls required to partition top-left and bottom-right cells in Python

Arnab Chakraborty
Updated on 26-Dec-2020 10:31:50

218 Views

Suppose we have a 2d binary matrix where 0 represents empty cell and 1 represents a wall. We have to find the minimum number cells that need to become walls so that there will be no path between top−left cell and bottom-right cell. We cannot put walls on the top−left cell and the bottom−right cell. We can move only left, right, up and down not diagonally.So, if the input is like0000010001100000then the output will be 2, 0100010001100010To solve this, we will follow these steps −R := row count of matrix, C := column count of matrixvisited := a new settin ... Read More

Program to count number of distinct characters of every substring of a string in Python

Arnab Chakraborty
Updated on 25-Dec-2020 06:01:45

307 Views

Suppose we have a lowercase string s, we have to find the sum of the count of characters that are distinct in every substring of s. If the answer is very large then return result mod 10^9+7.So, if the input is like s = "xxy", then the output will be 6, as the substrings and their counts are −"x" : 1"x" : 1"y" : 1"xx" : 0 (as "x" is not distinct)"xy" : 2"xxy" : 1 ("as "x" is not distinct)To solve this, we will follow these steps −m := 10^9 + 7prev_seen := a new empty mapans := 0Define ... Read More

Program to find number of operations needed to convert list into non-increasing list in Python

Arnab Chakraborty
Updated on 25-Dec-2020 06:00:33

152 Views

Suppose we have a list of numbers called nums. Now let us consider an operation where we take two consecutive values and merge it into one value by taking their sum. We have to find the minimum number of operations required so that the list turns into non−increasing.So, if the input is like nums = [2, 6, 4, 10, 2], then the output will be 2, as we can merge [2, 6] to get [8, 4, 10, 2] and then merge [8, 4] to get [12, 10, 2].To solve this, we will follow these steps −if nums is empty, thenreturn ... Read More

Program to count number of trailing zeros of minimum number x which is divisible by all values from 1 to k in Python

Arnab Chakraborty
Updated on 25-Dec-2020 05:56:38

143 Views

Suppose we have a number k, now consider the smallest positive integer value x where all values from 1 to k divide evenly. In other words, consider the smallest value x where x is divisible by all numbers from 1 through k. We have to find the number of trailing zeroes in x.So, if the input is like k = 6, then the output will be 0, as the smallest x here is 60, 60 can be divided using 1, 2, 3, 4, 5 and 6. There is only one trailing zeroes in 60.To solve this, we will follow these steps −res := 0x := 1while x * 5

Program to Find Out the Maximum Final Power of a List in Python

Arnab Chakraborty
Updated on 23-Dec-2020 07:03:03

234 Views

Suppose, we have a list and the power of a list is defined by the sum of (index + 1) * value_at_index over all indices. Alternatively, we can represent it like this −$$\displaystyle\sum\limits_{i=0}^{n-1} (i+1)\times list[i]$$Now, we have a list nums that has N positive integers. We can select any singular value in the list, and move (not swap) it to any position, it can be shifted to the beginning of the list, or to the end. We can also choose to not move any position at all. We have to find the maximum possible final power of the list. The ... Read More

Advertisements