Found 10476 Articles for Python

Program to check number of ways we can move k times and return back to first place in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:26:10

162 Views

Suppose we are at position 0 of n length list, and on each step, we can move right one place or left one place (not exceeding bounds), or stay at the same position. Now if we can take exactly k steps, then we have to find how many unique walks we can take and reach back to index 0. If the answer is very large return it mod 10^9 + 7.So, if the input is like n = 7 k = 4, then the output will be 9, as the actions are- [Right, Right, Left, Left], [Right, Left, Right, Left], [Stay, ... Read More

Program to find number of steps to solve 8-puzzle in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:24:32

15K+ Views

Suppose we have a 3x3 board of where all numbers are in range 0 to 8 and no repeating numbers are there. Now, we can swap the 0 with one of its 4 neighbors, and we are trying to solve it to get all arranged sequence, we have to find minimum number of steps required to reach the goal.So, if the input is like312475680then the output will be 4To solve this, we will follow these steps −Define a function find_next() . This will take nodemoves := a map defining moves as a list corresponding to each value {0: [1, 3], ... Read More

Program to check whether we can form 24 by placing operators in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:22:21

133 Views

Suppose we have a list of four numbers, each numbers are in range 1 to 9, in a fixed order. Now if we place the operators +, -, *, and / (/ denotes integer division) between the numbers, and group them with brackets, we have to check whether it is possible to get the value 24 or not.So, if the input is like nums = [5, 3, 6, 8, 7], then the output will be True, as (5 * 3) - 6 + (8 + 7) = 24.To solve this, we will follow these steps −Define a function recur() . ... Read More

Program to find number of days it will take to burn all trees in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:20:03

868 Views

Suppose we have a 2D matrix represents a forest where there are three types of cells: 0 empty cell 1 tree cell 2 tree on fire cell Every day, a tree catches fire when there is an adjacent (top, down, left, right, not diagonal) tree is on fire. We have to find the number of days it would take for every tree to be on fire. If that is not possible return -1.So, if the input is like121101111then the output will be 4, To solve this, we will follow these steps −ans := 0twos := a new listfor i in ... Read More

Program to count number of paths whose sum is k in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:16:32

213 Views

Suppose we have a binary tree and another value k, we have to find the number of unique node to sub child paths are there which sums to k.So, if the input is likeand k = 5, then the output will be 2, as the paths are [2, 3] and [1, 4]To solve this, we will follow these steps −count := a map initially holds value 1 for key 0ans := 0, prefix := 0Define a function dfs() . This will take nodeif node is not null, thenprefix := prefix + value of nodeans := ans + (count[prefix - target], ... Read More

Program to check whether we can color a tree where no adjacent nodes have the same color or not in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:15:00

242 Views

Suppose we have a binary tree where the value of each node represents its color. There are at most 2 colors in a tree. We have to check whether it is possible to swap the colors of the nodes any number of times so that no two connected nodes have the same color.So, if the input is likethen the output will be True as we can getTo solve this, we will follow these steps −colors := an empty mapprop := an empty mapDefine a function dfs() . This will take node, and flagif node is null, thenreturncolors[value of node] := ... Read More

Program to find the final ranking of teams in order from highest to lowest rank in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:13:14

362 Views

Suppose we have a list of strings called votes, here each entry is in lowercase letters and they are representing votes on candidates in order from highest to lowest preference. Here the rank of a candidate depends first by the number of votes received on the highest preference. Now if there are ties, we shall check the number of votes received on next highest preference, and so on. If there are still ties, then they will be ranked alphabetically. So we have to find the final ranking of the teams in order from highest to lowest ranked.So, if the input ... Read More

Program to count number of switches that will be on after flipping by n persons in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:12:05

468 Views

Suppose we have a number n, and there are n switches in a room, and all switches are off. Now n people who flip switches as follows −Person 1 flips all switches that are multiples of 1 (so all of the switches).Person 2 flips switches that are multiples of 2 (2, 4, 6, ...)Person i flips switches that are multiples of i.We have to find the number of switches that will be on at the end.So, if the input is like n = 5, then the output will be 2, as initially all are off [0, 0, 0, 0, 0], ... Read More

Program to count number of surrounded islands in the matrix in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:10:50

578 Views

Suppose we have a binary matrix. Where 1 represents land and 0 represents water. As we know an island is a group of 1s that are grouped together whose perimeter is surrounded by water. We have to find the number of completely surrounded islands.So, if the input is likethen the output will be 2, as there are three islands, but two of them are completely surrounded.To solve this, we will follow these steps −Define a function dfs() . This will take i, jif i and j are not in range of matrix, thenreturn Falseif matrix[i, j] is same as 0, ... Read More

Program to find sum of all numbers formed by path of a binary tree in python

Arnab Chakraborty
Updated on 02-Dec-2020 05:08:32

306 Views

Suppose we have a binary tree where each node is containing a single digit from 0 to 9. Now each path from the root to the leaf represents a number with its digits in order. We have to find the sum of numbers represented by all paths in the tree.So, if the input is likethen the output will be 680 as 46 (4 → 6), 432 (4 → 3 → 2), 435 (4 → 3 → 5), and their sum is 913.To solve this, we will follow these steps −Define a function solve() . This will take root, string:= blank ... Read More

Advertisements