Found 10476 Articles for Python

Program to find sum of widths of all subsequences of list of numbers in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:29:06

152 Views

Suppose we have a list of numbers called nums, The width of a sequence of numbers as the difference between the maximum and minimum number in the sequence. We have to find the sum of widths of all subsequences of nums. If the answer is very large then mod the result by 10^9+7.So, if the input is like nums = [7, 4, 9], then the output will be 15, as we have the subsequences like: [7], [4], [9], [7, 4], [7, 9], [4, 9], [7, 4, 9] and So the widths are 0, 0, 0, 3, 2, 5, 5, so ... Read More

Program to find number of sublists containing maximum and minimum after deleting only one element in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:27:51

223 Views

Suppose we have a list of numbers called nums and we can delete at most one element in the list. We have to find the maximum number of sublists that contain both the maximum and minimum values of the resulting list.So, if the input is like nums = [3, 2, 6, 2, 4, 10], then the output will be 8, as if we remove 10 we will get [3, 2, 6, 2, 4] and there are eight sublists where it contains both the max and the min −[2, 6][6, 2][2, 6, 2][3, 2, 6][6, 2, 4][2, 6, 2, 4][3, 2, ... Read More

Program to find size of sublist where product of minimum of A and size of A is maximized in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:25:49

153 Views

Suppose we have a list of numbers called nums and another value pos. We have to find a sublist A of nums that includes the index pos such that (minimum of A) * (size of A) is maximized then return the value.So, if the input is like nums = [-2, 2, 5, 4] pos = 3, then the output will be 8, as the best sublist is [5, 4], because (5, 4) = 4 and its size is 2 we have 4 * 2 = 8.To solve this, we will follow these steps −ans := A[pos], m := A[pos]i := ... Read More

Program to expand string represented as n(t) format in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:23:51

366 Views

Suppose we have a string s, this is encoding a longer string. s is represented as the concatenation of n(t), n(t) represents the concatenation of t, n times, and t is either a regular string or it is another encoded string recursively. We have to find the decoded version of s.So, if the input is like s = "3(pi)2(3(am))0(f)1(u)", then the output will be "pipipiamamamamamamu"To solve this, we will follow these steps −i := 0Define a function parse() . This will takeans := a new listwhile i < size of s and s[i] is not same as ")", doif s[i] ... Read More

Program to find minimum cost to increase heights of trees where no adjacent tree has same height in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:14:54

244 Views

Suppose we have a list of numbers called heights that represents the height of plants and we have another list of values called costs that represents the price needed to increase height of a plant by one. We have to find the smallest cost to make each height in the heights list different from adjacent heights.So, if the input is like heights = [3, 2, 2] costs = [2, 5, 3], then the output will be 3, as we can increase the last height by 1, which costs 3.To solve this, we will follow these steps −Define a function dp() ... Read More

Program to find maximum value of k for which we can maintain safe distance in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:13:10

163 Views

Suppose we have a binary matrix. Here 0 signifies an empty cell, and a 1 signifies a cell with a person. The distance between two cells is the maximum value between the difference in x coordinates and the difference in y coordinates. Now matrix is considered safe with a factor k if there is an empty square such that the distance from the cell to each person in the matrix, and each side of the matrix is all greater or equal to k. We have to find the maximum value of factor k for which we can be safe.So, if ... Read More

Program to find size of smallest sublist whose sum at least target in Python

Arnab Chakraborty
Updated on 23-Dec-2020 06:10:54

205 Views

Suppose we have a list of numbers called nums, and an another input called target, we have to find the size of the shortest sublist such that its sum value is same as target or larger. If there is no such sublist then return -1.So, if the input is like nums = [2, 11, -4, 17, 4] target = 19, then the output will be 2, as we can select [17, 4] to get sum of at least 19.To solve this, we will follow these steps −ps := a list with only one element 0for each num in nums, doinsert ... Read More

Program to remove all islands which are completely surrounded by water in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:55:56

734 Views

Suppose we have a binary matrix where 1 represents land and 0 represents water. And an island is a group of 1's that are surrounded by 0s (water) or by the edges. We have to find all of the islands that are completely surrounded by water and modify them into 0s. As we know an island is completed surrounded by water if all of the neighbors (horizontal and vertical not diagonal) are 0s (none of the neighbors are edges).So, if the input is like10000110011001100001then the output will be10000000000000000001To solve this, we will follow these steps −row := row count of ... Read More

Program to find length of shortest supersequence in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:52:38

174 Views

Suppose we have two strings s and t. We have to find the length of the shortest string that has both s and t as subsequences.So, if the input is like s = "pipe" t = "people", then the output will be 7, as one possible supersequence is "pieople".To solve this, we will follow these steps −m := size of s, n := size of ttable := a table of size (n + 1) x (m + 1) and fill with 0for i in range 0 to m, dofor j in range 0 to n, doif i is same as ... Read More

Program to find distance of shortest bridge between islands in Python

Arnab Chakraborty
Updated on 22-Dec-2020 08:50:57

453 Views

Suppose we have a binary matrix, where 0 represents water and 1 represents the land. An island is a group of connecting 1s in 4 directions. Islands are either surrounded by 0s (water) or by the edges. We have to find the length of shortest bridge that connects two islands.So, if the input is like001101100then the output will be 1. This will connect (1, 0) to (1, 2) points.To solve this, we will follow these steps −row := row count of matrixcol := column count of matrixDefine a function dfs() . This will take i, j, sif (i, j) is ... Read More

Advertisements