Found 10476 Articles for Python

Program to arrange cards so that they can be revealed in ascending order in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:33:31

471 Views

Suppose we have a list of cards, and we want to order the cards in a way so that they are revealed in ascending order. As we know, the cards are revealed in this manner: 1. The top most card is removed and revealed and then next card is gone to the back. 2. Step 1 is repeated until there's no more cards. We have to find an ordering of the cards such that they are revealed in ascending order.So, if the input is like cards = [1, 2, 3, 4, 5, 6, 7, 8], then the output will be ... Read More

Program to partition two strings such that each partition forms anagram in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:32:08

530 Views

Suppose we have two non-empty strings s and t that are of the same length. We have to partition them into substrings such that each pair of s and t substring is the same size and they are the anagrams of each other. Now find the cut indexes such that it results in the maximum number of cuts of s and t. If no result is found, then return empty list.So, if the input is like s = "bowcattiger" t = "owbactietgr", then the output will be [0, 3, 5, 6, 10], as we can partition the string into 5 ... Read More

Program to find sum of the sum of all contiguous sublists in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:30:30

418 Views

Suppose we have a list of numbers called nums, now consider every contiguous subarray. Sum each of these subarray and return the sum of all these values. Finally, mod the result by 10 ** 9 + 7.So, if the input is like nums = [3, 4, 6], then the output will be 43, as We have the following subarrays − [3] [4] [6] [3, 4] [4, 6] [3, 4, 6] The sum of all of these is 43.To solve this, we will follow these steps −N:= size of numsans:= 0for i in range 0 to size of nums, don:= nums[i]ans ... Read More

Program to check we can reach end of list by starting from k in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:27:54

372 Views

Suppose we have a list of numbers called nums and another number k. If we start at index k and at any index i, we can go either left or right by exactly nums[i] number of steps. We have to check whether we can reach the end of the list or not.So, if the input is like nums = [0, 0, 2, 1, 3, 3, 1, 1] k = 2, then the output will be True, as if we start at index 2, then jump to index 4 and then jump to the last index 7.To solve this, we will ... Read More

Program to find how many ways we can climb stairs (maximum steps at most k times) in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:28:55

249 Views

Suppose we have a staircase with n steps and we also have another number k, initially we are at stair 0, and we can climb up either 1, 2 or 3 steps at a time. but we can only climb 3 stairs at most k times. Now we have to find the number of ways we can climb the staircase.So, if the input is like n = 5, k = 2, then the output will be 13, as there are different ways we can climb the stairs −[1, 1, 1, 1, 1][2, 1, 1, 1][1, 2, 1, 1][1, 1, 2, ... Read More

Program to find how many ways we can climb stairs in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:25:32

333 Views

Suppose we have a staircase with n steps, and we can climb up either 1 or 2 steps at a time. We have to define a function that returns the number of unique ways we can climb the staircase.The order of the steps should not be changed, so each different order of steps counts as a way. If the answer is very large then mod the result by 10^9 + 7So, if the input is like n = 5, then the output will be 8, as there are 8 unique ways −1, 1, 1, 1, 12, 1, 1, 11, 2, ... Read More

Program to find minimum number of busses are required to pass through all stops in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:35:51

2K+ Views

Suppose we have a list of numbers called nums and that is showing the bus stops on a line where nums[i] shows the time a bus must arrive at station i. Now that buses can only move forward, we have to find the minimum number of buses that are needed to pass through all the stops.So, if the input is like nums = [1, 2, 7, 9, 3, 4], then the output will be 2, as one bus can take stops [1, 2, 3, 4] and another can do [7, 9].To solve this, we will follow these steps−ans := 0seen ... Read More

Program to add two binary strings, and return also as binary string in C++

Arnab Chakraborty
Updated on 05-Oct-2020 12:19:16

1K+ Views

Suppose we have two binary strings a and b, we have to add these binary numbers and find their sum, also as a string.So, if the input is like a = "10110", b = "10010", then the output will be "101000".To solve this, we will follow these steps −ret := empty stringna := size of a, nb := size of bi := na - 1, j := nb - 1carry := 0while (i >= 0 or j >= 0), do:addA := (if i >= 0, then a[i] - ASCII of '0', otherwise 0)addB := (if j >= 0, then b[j] ... Read More

Program to find maximum amount we can get by taking different items within the capacity in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:15:03

577 Views

Suppose we have two lists called weights and values which are of same length and another number called capacity k. Here weights[i] and values[i] shows the weight and value of the ith item. Now, we can take at most k capacity weights, and that we can only take at most one copy of each item, we have to find the maximum amount of value we can get.So, if the input is like weights = [2, 3, 4], values = [2, 6, 4], capacity = 6, then the output will be 8To solve this, we will follow these steps −n:= size ... Read More

Program to find the sum of elements that forms a Z shape on matrix in Python

Arnab Chakraborty
Updated on 05-Oct-2020 12:12:57

452 Views

Suppose we have one n x n matrix M, we have to find the sum of all elements that form a Z shape in the matrix.So, if the input is like432918256then the output will be 23, as elements are [4+3+2+1+2+5+6] = 23.To solve this, we will follow these steps −n := row count of matrixif n

Advertisements