Found 10476 Articles for Python

Program to find contiguous intervals of a unique array in Python

Arnab Chakraborty
Updated on 05-Oct-2020 14:58:01

673 Views

Suppose we have a list of unique numbers called nums. We have to find a sorted 2D matrix of numbers where each list represents an inclusive interval summarizing number that are contiguous in nums.So, if the input is like nums = [10, 11, 12, 15, 16, 17, 28, 30], then the output will be [[10, 12], [15, 17], [28, 28], [30, 30]], as in the list [10 to 12], [15 to 17] are contiguous, and 28 and 30 are there, they are represented as [28 to 28] and [30 to 30].To solve this, we will follow these steps−sort the list ... Read More

Program to check string contains consecutively descending string or not in Python

Arnab Chakraborty
Updated on 05-Oct-2020 14:42:50

306 Views

Suppose we have a string s with some digits, we have to check whether it contains consecutively descending integers or not.So, if the input is like s = "99989796", then the output will be True, as this string is holding [99, 98, 97, 96]To solve this, we will follow these steps−Define a function helper() . This will take pos, prev_numif pos is same as n, thenreturn Truenum_digits := digit count of prev_numfor i in range num_digits - 1 to num_digits, doif s[from index pos to pos+i-1] and numeric form of s[from index pos to pos+i-1]) is same as prev_num - ... Read More

Program to check we can visit any city from any city or not in Python

Arnab Chakraborty
Updated on 05-Oct-2020 14:25:52

801 Views

Suppose we have n cities represented as a number in range [0, n) and we also have a list of one-way roads that connects one city to another. We have to check whether we can reach any city from any city.So, if the input is like n = 3, roads = [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]], then the output will be True, as You can go 0 to 1 and 1 to 0To solve this, we will follow these steps−Define a function dfs() . This will take i, visited, gmark i as visitedfor ... Read More

Program to check whether a binary tree is complete or not in Python

Arnab Chakraborty
Updated on 05-Oct-2020 14:17:57

462 Views

Suppose we have a binary tree; we have to check whether this is a complete binary tree or not. As we know in a complete binary tree the levels are filled with nodes except possibly the last and all nodes in the last level are as far left as possible.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps−q := a double ended queueinsert root at the end of qflag := Falsewhile q is not empty, dotemp := element after deleting from left of qif temp is null, thenflag := Trueotherwise when ... Read More

Program to count minimum number of operations to flip columns to make target in Python

Arnab Chakraborty
Updated on 05-Oct-2020 14:06:56

218 Views

Suppose we have a matrix M and a target matrix T with the same number of rows and columns. Now suppose an operation where we flip a particular column in matrix so that all 1s will be converted to 0s and all 0s will be converted to 1s. So if we can reorder the matrix rows for free, find the minimum number of operations required to turn M into T. If there is no solution, then return -1.So, if the input is like M =001011T =011011then the output will be 1, as first reorder the rows to−001110And then flip column ... Read More

Program to count number of operations required to all cells into same color in Python

Arnab Chakraborty
Updated on 05-Oct-2020 13:53:12

224 Views

Suppose we have a two-dimensional matrix M. Now in each cell contains a value that represents its color, and adjacent cells (top, bottom, left, right) with the same color are to be grouped together. Now, consider an operation where we set all cells in one group to some color. Then finally find the minimum number of operations required so that every cell has the same color. And when the color is transformed, it cannot be set again.So, if the input is like222211112321Then the output will be 2, as We can fill the group with 2 as color into 1 and ... Read More

Program to find maximum number of coins we can collect in Python

Arnab Chakraborty
Updated on 05-Oct-2020 13:45:25

2K+ Views

Suppose we have a 2D matrix where each cell stores some coins. If we start from [0, 0], and can only move right or down, we have to find the maximum number of coins we can collect by the bottom right corner.So, if the input is like14220005then the output will be 14, as we take the path: [1, 4, 2, 2, 5]To solve this, we will follow these steps−for r in range 1 to row count of A, doA[r, 0] := A[r, 0] + A[r-1, 0]for c in range 1 to column count of A, doA[0, c] := A[0, c] ... Read More

Program to find largest sum of any path of a binary tree in Python

Arnab Chakraborty
Updated on 05-Oct-2020 13:19:36

152 Views

Suppose we have a binary tree, we have to find the largest sum of any path that goes from the root node to the leaf node.So, if the input is likethen the output will be 29 as from root, if we follow the path 5-

Program to find circular greater element to the right in Python

Arnab Chakraborty
Updated on 05-Oct-2020 13:09:30

170 Views

Suppose we have a list of numbers called nums. We have to find a new list of the same length where the value at index i is assigned to the next element greater than nums[i] to its right, circling back to the front of the list when required. If there is no number that is greater, then it should be set to -1.So, if the input is like [4, 5, 1, 3], then the output will be [5, -1, 3, 4]To solve this, we will follow these steps−n := size of astack := a stack, insert 0 initially, res := ... Read More

Program to find the middle node of a singly linked list in Python

Arnab Chakraborty
Updated on 05-Oct-2020 13:04:38

290 Views

Suppose we have a singly linked list node, we have to find the value of the middle node. And when there are two middle nodes, then we will return the second one. We have to try to solve this in single pass.So, if the input is like [5, 9, 6, 4, 8, 2, 1, 4, 5, 2], then the output will be 2.To solve this, we will follow these steps−p:= noded:= 0, l:= 0while node is not null, doif d is not same as 2, thennode:= next of nodel := l + 1, d := d + 1otherwise, p:= next ... Read More

Advertisements