Found 10784 Articles for Python

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

Arnab Chakraborty
Updated on 06-Oct-2020 05:59:22

540 Views

Suppose we have binary tree; we have to check whether it is a binary search tree or not. As we know a BST has following properties −all nodes on its left subtree is smaller than current node valueall nodes on its right subtree is larger than current node valuethese properties hold recursively for all nodesSo, if the input is likethen the output will be TrueTo solve this, we will follow these steps −x := a list of inorder traversal sequence of tree elementsif x is sorted, thenreturn truereturn falseLet us see the following implementation to get better understanding −Example Live Democlass ... Read More

Program to remove all nodes from BST which are not in range in Python

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

129 Views

Suppose we have a BST, an two values low and high, we have to delete all nodes that are not between [low, high] (inclusive).So, if the input is likelow = 7 high = 10, then the output will beTo solve this, we will follow these steps −Define a function solve() . This will take root, low, highif root is null, thenreturnif low > data of root, thenreturn solve(right of root, low, high)if high < data of root, thenreturn solve(left of root, low, high)right of root := solve(right of root, low, high)left of root := solve(left of root, low, high)return rootLet ... Read More

Program to check whether different brackets are balanced and well-formed or not in Python

Arnab Chakraborty
Updated on 06-Oct-2020 05:53:03

225 Views

Suppose we have a string of brackets (round, curly, and square), we have to check whether the brackets are balanced (well-formed) or not.So, if the input is like s = "([()()]{[]})()", then the output will be TrueTo solve this, we will follow these steps −stack := a new listd := a hash map with key-value pairs ('}', '{'), (')', '('), (']', '[')for each character c in s, doif c is any one of '}])', thenif stack is empty or top of stack is not same as d[c], thenreturn Falsepop from stackotherwise, push c into stackreturn true when stack is empty, ... Read More

Program to check whether parentheses are balanced or not in Python

Arnab Chakraborty
Updated on 06-Oct-2020 05:49:35

185 Views

Suppose we have a string s consisting of parenthesis "(" and ")". We have to check whether the parentheses are balanced or not.So, if the input is like s = "(()())(())", then the output will be TrueTo solve this, we will follow these steps −num_open := 0for each character c in s, doif c is same as ')', thenif num_open < 0, thennum_open := num_open - 1otherwise, return Falseotherwise, num_open := num_open + 1return inverse of num_openLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s):       num_open = 0   ... Read More

Program to count number of unique binary search tree can be formed with 0 to n values in Python

Arnab Chakraborty
Updated on 06-Oct-2020 05:47:30

190 Views

Suppose we have one number n, we have to find the number of unique BSTs we can generate with numbers from [0, n). If the answer is very large mod the result by 10^9+7So, if the input is like n = 3, then the output will be 5To solve this, we will follow these steps −numer := 1denom := n + 1for i in range 1 to n, donumer := numer * n + inumer := numer mod mdenom := denom * idenom := denom mod mnumer := numer * (denom^(m-2)) mod mreturn numer mod mLet us see the following ... Read More

Program to find contiguous intervals of a unique array in Python

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

533 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

238 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

698 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

364 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

157 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

Advertisements