Found 10784 Articles for Python

Program to check two trees are exactly same based on their structure and values in Python

Arnab Chakraborty
Updated on 21-Oct-2020 12:19:58

83 Views

Suppose we have two binary trees, we have to check whether they are exactly same in terms of their structures and values or not. We can say them as twin trees.So, if the input is likethen the output will be True for the first pair, false for the second pair and third pair as for second and third items are different and the structures are different respectively.To solve this, we will follow these steps −Define a method solve(), this will take two rootsif root0 is null and root1 is null, thenreturn Trueif root0 is null or root1 is null, thenreturn ... Read More

Program to find possible number of palindromes we can make by trimming string in Python

Arnab Chakraborty
Updated on 21-Oct-2020 12:18:07

172 Views

Suppose we have a string s, we have to find the number of ways we can obtain a palindrome by trimming the left and right sides of s.So, if the input is like s = "momo", then the output will be 6, as You can get ["mom", "omo", "o", "o", "m", "m", "o")To solve this, we will follow these steps −Define a function expand() . This will take i, j, sc := 0while i >= 0 and j < size of s and s[i] is same as s[j], doi := i − 1, j := j + 1c := c ... Read More

Program to traverse binary tree using list of directions in Python

Arnab Chakraborty
Updated on 21-Oct-2020 12:14:42

199 Views

Suppose we have a binary tree and a list of strings moves consisting of "R"(Right), "L"(Left) and "U"(Up). Starting from root, we have to traverse the tree by performing each move in moves where: "R" indicates traverse to the right child. "L" indicates traverse to the left child. "U" indicates traverse to its parent.So, if the input is like["R", "R", "U", "L"], then the output will be 3To solve this, we will follow these steps −past := a new listfor each move in moves, doinsert root at the end of pastif move is same as "L", thenroot := left of ... Read More

Program to find sum of all elements of a tree in Python

Arnab Chakraborty
Updated on 21-Oct-2020 12:12:43

747 Views

Suppose we have a binary tree containing some values, we have to find the sum of all values in the tree.So, if the input is likethen the output will be 14To solve this, we will follow these steps −Define a function recurse() . This will take nodeval := value of nodeif left of node is not null, thenval := val + recurse(left of node)if right of node is not−null, thenval := val + recurse(right of node)return valFrom the main method, do the following −if not root is non−zero, thenreturn 0return recurse(root)Let us see the following implementation to get better understanding ... Read More

Program to find minimum distance that needs to be covered to meet all person in Python

Arnab Chakraborty
Updated on 21-Oct-2020 12:08:16

200 Views

Suppose we have a 2D matrix there are few values like below −0 represents an empty cell.1 represents a wall.2 represents a person.Here a person can walk any of these four directions (up, down, left and right). We have to find a cell that is not wall such that it minimizes the total travel distance each person has to walk to and finally find the distance.So, if the input is like201010120022then the output will be 7 as the best meeting point is the bottom right corner.To solve this, we will follow these steps −twos := a new map, costs := ... Read More

Program to find minimum number of cells it will take to reach bottom right corner in Python

Arnab Chakraborty
Updated on 21-Oct-2020 12:04:30

143 Views

Suppose we have a 2D grid representing a maze where 0 is for empty space, and 1 is the wall. We will start at grid[0, 0], we have to find the minimum number of squares it would take to get to bottom right corner of the grid. If we cannot reach, return −1.So, if the input is like000100100then the output will be 5To solve this, we will follow these steps −R := row count of grid, C := column count of gridq := [0, 0, 1] when A[0, 0] is 1 otherwise a new listA[0, 0] := 1for each (r, ... Read More

Program to schedule tasks to take smallest amount of time in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:59:05

166 Views

Suppose we have a list of values called tasks where each different value represents a different task type, and we also have a non-negative integer k. Each task wants one minute to complete, but we must wait k minutes between doing two tasks of the same type. At any time, we can be doing a task or waiting. We have to find the smallest amount of time it takes to complete all the tasks.So, if the input is like nums = [2, 2, 2, 3, 3, 2], k = 1, then the output will be 7, as the optimal ordering ... Read More

Program to find number of given operations required to reach Target in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:57:08

322 Views

Suppose we have two values start and end, we have to find the minimum number of operations needed to convert start to end by using these operations −Decrement by 1Multiply by 2So, if the input is like start = 2, end = 7, then the output will be 3, as we can multiply 2 to get 4, then multiply 2 to get 8 and then subtract 1 to get 7.To solve this, we will follow these steps −ans := 0Do the following infinitely, doif end

Program to check whether given tree is symmetric tree or not in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:54:58

189 Views

Suppose we have one binary tree. We have to check whether the tree is symmetric tree or not. A tree will be said to be symmetric if it is same when we take the mirror image of it. From these two trees, the first one is symmetric, but second one is not.To solve this, we will follow these steps.We will call following steps recursively. The function will be solve(root, root)if the node1 and node2 are empty, then return trueif either node1 or node2 is empty, then return falsereturn true when node1.val = node2.val and solve(node1.left, node2.right) and solve(node1.right, node2.left)Let us ... Read More

Program to check whether two trees can be formed by swapping nodes or not in Python

Arnab Chakraborty
Updated on 21-Oct-2020 11:50:02

103 Views

Suppose we have two trees, we have to check whether we can transform first tree into second one by swapping any node's left and right subtrees any number of times.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −que1 := a queue with root0 initiallyque2 := a queue with root1 initiallywhile que1 and que2 are not empty, dotemp1 := a new list, temp2 := a new listvalues1 := a new list, values2 := a new listif que1 and que2 are containing different number of elements, thenreturn Falsefor i in range 0 ... Read More

Advertisements