Found 10784 Articles for Python

Program to find maximum value we can get in knapsack problem by taking multiple copies in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:39:39

485 Views

Suppose we have two lists of same length they are called weights and values, and we also have another value capacity. Here weights[i] and values[i] represent the weight and value of the ith item. If we can take at most capacity weights, and that we can take any number of copies for each item, we have to find the maximum amount of value we can get.So, if the input is like weights = [1, 2, 3], values = [1, 5, 3], capacity = 5, then the output will be 11To solve this, we will follow these steps −Define a function ... Read More

Program to count number of ways we can distribute coins to workers in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:36:17

260 Views

Suppose we have two lists of positive numbers called coins and salaries. Here coins[i] indicates the value for coin i and salaries[j] indicates the least amount of salary required to pay for worker j. Now suppose we have one coin per type and we must give each worker exactly one coin, we have to compute the number of ways to give coins to each worker. Here two ways are different if some worker receives one type of coin in one way but a different type of coin in the other way. If the result is very large return result mod ... Read More

Program to count how many ways we can divide the tree into two trees in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:31:58

124 Views

Suppose we have a binary tree containing values 0, 1 and 2. The root has at least one 0 node and one 1 node. Now suppose there is an operation where we delete an edge in the tree and the tree becomes two different trees. We have to find the number of ways we can delete one edge such that none of the two trees contain both a 0 node and a 1 node.So, if the input is likethen the output will be 1 as we can only delete the 0 to 2 edge.To solve this, we will follow these ... Read More

Program to find leaf and non-leaf nodes of a binary tree in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:27:36

896 Views

Suppose we have a binary tree, we have to find a list of two numbers where the first number is the count of leaves in the tree and the second number is the count of non-leaf nodes.So, if the input is likethen the output will be (3, 2), as there are 3 leaves and 2 non-leaf nodes.To solve this, we will follow these steps −if n is null, thenreturn (0, 0)if left of n is null and right of n is null, thenreturn (1, 0)left := solve(left of n)right := solve(right of n)return (left[0] + right[0], 1 + left[1] + ... Read More

Program to check whether inorder sequence of a tree is palindrome or not in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:25:11

189 Views

Suppose we have a binary tree where each node contains a digit from 0-9, we have to check whether its in-order traversal is palindrome or not.So, if the input is likethen the output will be True, as its inorder traversal is [2, 6, 10, 6, 2].To solve this, we will follow these steps −if root is null, thenreturn Truestack := a new stackcurr := rootinorder := a new listwhile stack is not empty or curr is not null, dowhile curr is not null, dopush curr into stackcurr := left of currnode := popped element from stackinsert value of node at ... Read More

Program to check given string is anagram of palindromic or not in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:20:37

399 Views

Suppose we have a string s, we have to check whether any permutation of s is a palindrome or not.So, if the input is like s = "admma", then the output will be True, as we can rearrange "admma" to "madam" which is a palindrome.To solve this, we will follow these steps −c := a map holding each individual character count of scount := 0for each i in list of all values of c, doif i is odd, thenif count is same as 0, thencount := count + 1come out from the loopreturn Falsereturn TrueLet us see the following implementation ... Read More

Program to check linked list items are forming palindrome or not in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:19:16

93 Views

Suppose we have a linked list. We have to check whether the list elements are forming a palindrome or not. So if the list element is like [5, 4, 3, 4, 5], then this is a palindrome, but a list like [5, 4, 3, 2, 1] is not a palindrome.To solve this, we will follow these steps −fast := head, slow := head, rev := None and flag := 1if head is empty, then return truewhile fast and next of fast is availableif next of the next of fast is available, then set flag := 0 and break the loopfast ... Read More

Program to swap nodes of a linked list pair wise in C++

Arnab Chakraborty
Updated on 20-Oct-2020 07:17:01

160 Views

Suppose we have a linked list. We have to swap every two adjacent nodes (pair) and return its head. Here the constraint is that, we cannot modify the value of the nodes, only the node itself can be changed. So if the list is like [1, 2, 3, 4], then the resultant list will be [2, 1, 4, 3].To solve this, we will follow these steps −if head is not present, then return headfirst := head, second := next of head, dummy is one new node with value -1next of dummy := first, and prev := dummywhile second is not ... Read More

Program to count maximum number of distinct pairs whose differences are larger than target in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:14:13

201 Views

Suppose we have a list of numbers called nums and another value target. We have to find the maximum number of pairs where for each pair i < j, i and j are not in any other pair, and |nums[i] - nums[j]| >= target.So, if the input is like nums = [2, 4, 6, 10, 11], target = 5, then the output will be 2, as we can get pairs: (2, 6), (5, 10)To solve this, we will follow these steps −N := size of Asort the list Aans := 0j := N / 2for i in range 0 to ... Read More

Program to fill with color using floodfill operation in Python

Arnab Chakraborty
Updated on 20-Oct-2020 07:12:10

194 Views

Suppose we have a 2D grid, containing colors as strings "r", "g", and "b". We have to perform floodfill operation at row r, column c with the color target. As we know the Floodfill operation should replace all elements that are both connected to grid[r, c] (up/right/down/left) and have the same color as grid[r, c] with the same color as target.So, if the input is likeRRRRGBGBBthen the output will beGGGGGBGBBas the red cells connected to grid[0, 0] are replaced with green ("g").To solve this, we will follow these steps −define a new set seenoldcolor := matrix[r, c]Define a function dfs() ... Read More

Advertisements