Found 10784 Articles for Python

Program to find number of coins needed to make the changes with given set of coins in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:15:15

176 Views

Suppose we have coins of different denominations and a total amount of money amount. We have to define one function to compute the fewest number of coins that we need to make up that amount. When that amount of money cannot be accommodated by any combination of the coins, return -1. So if the input is [1, 2, 5], and the amount is 64, the output is 14. This is formed using 12*5 + 2 + 2 = 64.To solve this, we will follow these steps −if amount = 0, then return 0if minimum of coins array > amount, then ... Read More

Program to find number of coins needed to make the changes in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:13:36

859 Views

Suppose we have coins of different denominations (1, 5, 10, 25) and a total amount of money amount. We have to define one function to compute the fewest number of coins that we need to make up that amount. So if the input is 64, the output is 7. This is formed using 25 + 25 + 10 + 1 + 1 + 1 + 1 = 64.To solve this, we will follow these steps −if amount = 0, then return 0if minimum of coins array > amount, then return -1define one array called dp, of size amount + 1, ... Read More

Program to convert one list identical to other with sublist sum operation in Python

Arnab Chakraborty
Updated on 19-Oct-2020 15:11:05

90 Views

Suppose we have two lists l1 and l2, we have to make the lists equal by applying this operation repeatedly − Choose a sublist, and replace the whole sublist with its sum. Finally return the size of the longest resulting list possible after applying above operations. If there's no solution, return -1.So, if the input is like l1 = [1, 4, 7, 1, 2, 10] l2 = [5, 6, 1, 3, 10], then the output will be 4, as if we perform this operation as follows −Take l1's sublist [1, 4] we get [5, 7, 1, 2, 10]Take l1's sublist ... Read More

Program to check minimum number of characters needed to make string palindrome in Python

Arnab Chakraborty
Updated on 12-Oct-2020 12:04:58

408 Views

Suppose we have a string s, we have to find the minimum number of characters needed to be inserted so that the string becomes a palindrome.So, if the input is like s = "mad", then the output will be 2, as we can insert "am" to get "madam".To solve this, we will follow these steps −Define a function dp(). This will take i, jif i >= j, thenreturn 0if s[i] is same as s[j], thenreturn dp(i + 1, j - 1)otherwise, return minimum of dp(i + 1, j) and dp(i, j - 1) + 1From the main method, do the ... Read More

Program to find an ancestor which is common of two elements in a binary tree in Python

Arnab Chakraborty
Updated on 12-Oct-2020 12:01:59

192 Views

Suppose we have a binary tree, and we also have two numbers a and b, we have to find the value of the lowest node that has a and b as descendants. We have to keep in mind that a node can be a descendant of itself.So, if the input is likea = 6, b = 2, then the output will be 4To solve this, we will follow these steps −Define a method solve() this will take root and a, bif root is null, thenreturn -1if value of root is either a or b, thenreturn value of rootleft := solve(left ... Read More

Program to find minimum length of lossy Run-Length Encoding in Python

Arnab Chakraborty
Updated on 10-Oct-2020 15:03:03

180 Views

Suppose we have a lowercase string s and another value k. Now consider an operation where we perform a run-length encoding on a string by putting repeated successive characters as a count and character. So if the string is like "aaabbc" would be encoded as "3a2bc". Here we do not put "1c" for "c" since it only appears once successively. So we can first remove any k consecutive characters in s, then find the minimum length possible of the resulting run-length encoding.So, if the input is like s = "xxxxxyyxxxxxzzxxx", k = 2, then the output will be 6, as ... Read More

Program to find sum of longest sum path from root to leaf of a binary tree in Python

Arnab Chakraborty
Updated on 10-Oct-2020 14:19:52

135 Views

Suppose we have a binary tree, we have to find the sum of the longest path from the root to a leaf node. If there are two same long paths, return the path with larger sum.So, if the input is likethen the output will be 20.To solve this, we will follow these steps −Define a function rec() . This will take currif curr is null, thenreturn(0, 0)bigger := maximum of rec(left of curr) , rec(right of curr)return a pair (bigger[0] + 1, bigger[1] + value of curr)From the main method do the following −ret := rec(root)return the 1th index of ... Read More

Program to find the length of longest substring which has two distinct elements in Python

Arnab Chakraborty
Updated on 10-Oct-2020 13:57:29

497 Views

Suppose we have a string s, we have to find the length of the longest substring that contains at most 2 distinct characters.So, if the input is like s = "xyzzy", then the output will be 4, as "yzzy" is the longest substring with at most 2 unique characters.To solve this, we will follow these steps−start := 0c := a mapans := 0for end in range 0 to size of s, doc[s[end]] := c[s[end]] + 1while size of c > 2, doc[s[start]] := c[s[start]] - 1if c[s[start]] is 0, thendelete c[s[start]]start := start + 1ans := maximum of ans and ... Read More

Program to find length of longest sublist with given condition in Python

Arnab Chakraborty
Updated on 10-Oct-2020 13:51:11

238 Views

Suppose we have a list of numbers called nums, we have to find the length of the longest sublist where 2 * minimum of sublist > maximum of sublist.So, if the input is like nums = [10, 2, 6, 6, 4, 4], then the output will be 4, as the sublist [6, 6, 4, 4] is the longest sublist that holds the criteria as 2 * 4 > 6.To solve this, we will follow these steps−ret := 0define two double ended queues minq and maxql := 0, r := 0while r < size of nums, don := nums[r]while minq and ... Read More

Program to find length of longest palindromic substring in Python

Arnab Chakraborty
Updated on 10-Oct-2020 13:41:59

516 Views

Suppose we have a string S. We have to find the length of longest palindromic substring in S. We are assuming that the length of the string S is 1000. So if the string is “BABAC”, then the longest palindromic substring is “BAB” and length is 3.To solve this, we will follow these steps −Define one square matrix of order same as the length of string, and fill it with FalseSet the major diagonal elements as true, so DP[i, i] = True for all i from 0 to order – 1start := 0for l in range 2 to length of ... Read More

Advertisements