Found 10784 Articles for Python

Binary Tree Inorder Traversal in Python

Arnab Chakraborty
Updated on 28-Apr-2020 06:11:48

3K+ Views

Suppose we have a binary tree. We have to traverse this tree using the inorder traversal scheme without using recursion. So if the tree is likeThen the traversal will be [2, 5, 7, 10, 15, 20]To solve this, we will follow these steps −Create two array res and stack, set curr := rootRun one infinite loopwhile current is not nullpush curr into a stack, and set curr := left of currwhen the length of stack = 0, then return resnode := popped element from the stackinsert a value of node into rescurr := right of currExampleLet us see the following ... Read More

Decode Ways in Python

Arnab Chakraborty
Updated on 28-Apr-2020 06:06:16

891 Views

Suppose we have a message containing letters from A to Z is being encoded to numbers using the following mapping − 'A' → 1, 'B' → 2 ... 'Z' → 26. So if we have one non-empty string, containing only digits, then we have to find, in how many ways that can be decoded. So if the string is like “12”, then that can be made from “AB”, or “L”, so there are two possible ways. So the answer will be 2.To solve this, we will follow these steps −We will solve this using dynamic programming.n := length of sdp ... Read More

Combinations in C++

Arnab Chakraborty
Updated on 27-Apr-2020 14:16:54

738 Views

Suppose we have two integers n and k. We have to find all possible combinations of k numbers out of 1 ... n. So if n = 4 and k = 2, then the combinations will be [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]To solve this, we will follow these steps −We will use recursive function to solve this. the function solve() is taking n, k, temp array and start. The start is initially 1. This will act likeif size of temp array = k, then insert temp into res array, and returnfor i := ... Read More

Sort Colors in Python

Arnab Chakraborty
Updated on 27-Apr-2020 14:12:40

2K+ Views

Suppose we have an array with n objects. These are colored red, white, or blue, sort them in place so that the objects of the same color are adjacent. So with the colors in the order red, white and blue. Here, we will use the numbers like 0, 1, and 2 to represent the color red, white, and blue respectively. So if the array is like [2,0,2,1,1,0], then the output will be [0,0,1,1,2,2]To solve this, we will follow these steps −set low := 0, mid := 0 and high := length of array – 1while mid

Pow(x, n) in Python

Arnab Chakraborty
Updated on 27-Apr-2020 13:51:18

2K+ Views

Suppose we have two inputs x and n. x is a number in range -100.0 to 100.0, and n is a 32-bit signed integer. We have to find x to the power n without using library functions.So if the given inputs are x = 12.1, n = -2, then output will be 0.00683To solve this, we will follow these steps −power := |n| and res := 1.0while power is not 0if last bit of power is 1, then res := res * xx := x * xif n < 0return 1 / resreturn resExample(Python)Let us see the following implementation to ... Read More

Rotate Image in Python

Arnab Chakraborty
Updated on 27-Apr-2020 13:45:13

356 Views

Suppose we have one 2D matrix, that is representing one image. We have to rotate this image to 90 degrees clockwise. So if the image is like157963213Then the output will be291165337To solve this, we will follow these steps −Consider temp_mat = [], col := length of matrix – 1for col in range 0 to length of matrixtemp := []for row in range length of matrix – 1 down to -1add matrix[row, col] in tempadd temp into temp_matfor i in range 0 to length of matrixfor j in range 0 to length of matrixmatrix[i, j] := temp_mat[i, j]Example(Python)Let us see the ... Read More

Multiply Strings in C++

Arnab Chakraborty
Updated on 27-Apr-2020 13:14:03

7K+ Views

Suppose we have two numbers as a string. We have to multiply them and return the result also in a string. So if the numbers are “26” and “12”, then the result will be “312”To solve this, we will follow these steps −Taking two arguments x and y it indicates x divides yif x < -Infinity and y = 1, then return infinitya := |x|, b := |y| and ans := 0while a – b >= 0p := 0while a – (left-shifted b (left-shifted 1 p times)) >= 0p := p + 1a := a – (left shift b, p ... Read More

Valid Sudoku in Python

Arnab Chakraborty
Updated on 27-Apr-2020 13:08:03

5K+ Views

Suppose we have one 9x9 Sudoku board. We have to check whether that is valid or now. Only the filled cells need to be validated according to the following rules −Each row must contain the digits from 1-9 without repetition.Each column must contain the digits from 1-9 without repetition.Each of the 9 (3x3) sub-boxes of the grid must contain the digits from 1-9 without repetition.Suppose the Sudoku grid is like −537619598686348317266284195879This is valid.To solve this, we will follow these steps −for i in range 0 to 8create some empty dictionary called row, col and block, row_cube := 3 * (i ... Read More

Find First and Last Position of Element in Sorted Array in Python

Arnab Chakraborty
Updated on 27-Apr-2020 12:59:33

788 Views

Suppose we have an array of integers A. This is sorted in ascending order, we have to find the starting and ending position of a given target value. When the target is not found in the array, return [-1, -1]. So if the array is like [2, 2, 2, 3, 4, 4, 4, 4, 5, 5, 6], and target is 4, then the output will be [4, 7]To solve this, we will follow these steps −Initially res := [-1, -1], set low := 0, high := length of array Awhile low < highmid := low + (high – low)/2if A[mid] ... Read More

Search in Rotated Sorted Array in Python

Arnab Chakraborty
Updated on 27-Apr-2020 12:55:30

806 Views

Consider we have an array sorted in ascending order, and that is rotated at some pivot unknown to you beforehand. For example, [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2]. We have given a target value to the search. If we can get it in the array, then return its index, otherwise return -1. We can assume no duplicate exists in the array. So if the array is like [4, 5, 6, 7, 0, 1, 2], then the output will be 4. as the index of this element is present at index ... Read More

Advertisements