Found 10476 Articles for Python

Program to find shortest cycle length holding target in python

Arnab Chakraborty
Updated on 02-Dec-2020 04:50:46

398 Views

Suppose we have adjacency list of a directed graph, where each list at index i is represented connected nodes from node i. We also have a target value. We have to find the length of a shortest cycle that contains the target. If there is no solution return -1.So, if the input is liketarget = 3., then the output will be 3, as the cycle is nodes 1 -> 2 -> 3 -> 1. Note there is another cycle 0 -> 1 -> 2 -> 3 -> 0, but this is not shortest.To solve this, we will follow these steps ... Read More

Program to find total cost for completing all shipments in python

Arnab Chakraborty
Updated on 02-Dec-2020 04:47:52

301 Views

Suppose we have a list of lists called ports, where ports[i] represents the list of ports that port i is connected to. We also have another list of lists called shipments where each list of the sequence [i, j] which denotes there is a shipment request from port i to port j. And the cost to ship from port i to port j is the length of the shortest path from the two ports, we have to find the total cost necessary to complete all the shipments.So, if the input is like ports = [[1, 4], [2], [3], [0, 1], ... Read More

Program to convert a string to zigzag string of line count k in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:34:06

363 Views

Suppose we have a string s and another value k, We have to find a new string by taking each character from s and starting diagonally from top left to bottom right until reaching the kth line, then go up to top right, and so on.So, if the input is like s = "ilovepythonprogramming" k = 5, then the output will beTo solve this, we will follow these steps:line := a new mapcnt := 0delta := 1for each index i and character c in s, doinsert (c, i) at the end of line[cnt]cnt := cnt + deltaif cnt is same ... Read More

Program to find length of longest alternating path of a binary tree in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:30:08

278 Views

Suppose we have a binary tree, we have to find the longest path that alternates between left and right child and going down.So, if the input is likethen the output will be 5 as the alternating path is [2, 4, 5, 7, 8].To solve this, we will follow these steps:if root is null, thenreturn 0Define a function dfs() . This will take node, count, flagif node is not null, thenif flag is same as True, thena := dfs(left of node, count + 1, False)b := dfs(right of node, 1, True)otherwise when flag is same as False, thena := dfs(right of ... Read More

Program to find length of longest word that can be formed from given letters in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:27:28

332 Views

Suppose we have a list of words and a string called letters, we have to find the size of the longest word that can be made by rearranging the given letters. In the letters there may be asterisk character (*) it can match any character. And it is not necessary to use all the letters.So, if the input is like words = ["prince", "rice", "price", "limit", "hello"] letters = "*r**ce*", then the output will be 6, as the longest word we can make is "prince" the length is 6.To solve this, we will follow these steps:has := a map containing ... Read More

Program to count number of word concatenations are there in the list in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:25:59

188 Views

Suppose we have a list of strings; we have to find the number of words that are concatenations of other words also in the list. We can reuse words when concatenating and concatenate any number of times.So, if the input is like words = ["hello", "world", "helloworld", "famous", "worldfamous", "programming"], then the output will be 2, as "helloworld" is concatenation of "hello" and "world". "worldfamous" is concatenation of "world" and "famous".To solve this, we will follow these steps:trie := a new mapfor each word in words, dolayer := triefor each w in word, doif w is not in layer, thenlayer[w] ... Read More

Program to find nearest time by reusing same digits of given time in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:22:21

347 Views

Suppose we have a 24-hour string in "hh:mm" format, we have to find the next closest time that can be formed by reusing given digits. We can reuse digits from the given string as many times as we want.So, if the input is like s = "03:15", then the output will be 03:30, as the nearest time 03:30 that repeats the given digits.To solve this, we will follow these steps:use := a list with two digit hour and two digit mins valuespossible := a new setDefine a function backtrack() . This will take pathif size of path is same as ... Read More

Program to find minimum number of days to wait to make profit in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:18:33

266 Views

Suppose we have a list of prices representing the daily stock market prices of a company in chronological sequence. We have to find a same length list where the value at index i will be the minimum number of days we would have to wait until we make a profit. If there is no such way to make a profit the value should be 0.So, if the input is like prices = [4, 3, 5, 9, 7, 6], then the output will be [2, 1, 1, 0, 0, 0]To solve this, we will follow these steps:ans := a list of ... Read More

Program to find all possible strings typed using phone keypad in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:15:07

1K+ Views

Suppose we have a string containing digits from 2-9. We have to find all possible letter combinations that the number could generate. One mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does map some characters but no letters.12a b c3d e f4g h i5j k l6m n o7p q r s8t u v9w x y z*0#For an example, if the given string is “49”, then the possible strings will be ['gw', 'gx', 'gy', 'gz', 'hw', 'hx', 'hy', 'hz', 'iw', 'ix', 'iy', 'iz']To solve this, we will follow these steps:Define an array ... Read More

Program to check whether a board is valid N queens solution or not in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:12:22

876 Views

Suppose we have a n x n matrix represents a chess board. There are some 1s and 0s, where 1 represents a queen and 0 represents an empty cell. We have to check whether the board is valid solution to the N-Queen puzzle or not. As we know a board is a solution of valid N-queen solution where no two queens are attacking each other.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps:n := row count of matrixrows := a new set, cols := a new set, diags := a new ... Read More

Advertisements