Python Articles - Page 775 of 1048

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

305 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

739 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

197 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

273 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

350 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

313 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

Program to pack same consecutive elements into sublist in Python

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

576 Views

Suppose we have a list of numbers nums, we will pack consecutive elements of the same value into sublists. We have to keep in mind that there is only one occurrence in the list it should still be in its own sublist.So, if the input is like nums = [5, 5, 2, 7, 7, 7, 2, 2, 2, 2], then the output will be [[5, 5], [2], [7, 7, 7], [2, 2, 2, 2]]To solve this, we will follow these steps −if nums is empty, thenreturn a new listresult := a list with another list that contains nums[0]j := 0for ... Read More

Program to find one minimum possible interval to insert into an interval list in Python

Arnab Chakraborty
Updated on 19-Oct-2020 16:19:58

191 Views

Suppose we have a 2D list of numbers called intervals where each row represents [start, end] (inclusive) interval. For an interval [a, b] (a < b), its size is (b - a). We must add one interval to the given list such that, after merging all the intervals, we get exactly one range left. We have to find the minimum possible size of the added interval.So, if the input is like intervals = [[15, 20], [30, 50]], then the output will be 10, as we can add the interval [20, 30] which is the smallest possible interval.To solve this, we ... Read More

Program to find minimum cost to reduce a list into one integer in Python

Arnab Chakraborty
Updated on 19-Oct-2020 16:14:56

838 Views

Suppose we have a list of numbers called nums. We can reduce the length of nums by taking any two numbers, removing them, and appending their sum at the end. The cost of doing this operation is the sum of the two integers we removed. We have to find the minimum total cost of reducing nums to one integer.So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be 45, as we take 2 and 3 then remove to get [4, 5, 6, 5], then we take 4 and 5 then remove to ... Read More

Program to check two strings are 0 or 1 edit distance away or not in Python

Arnab Chakraborty
Updated on 19-Oct-2020 16:10:51

458 Views

Suppose we have two strings S and T we have to check whether they are one or zero edit distance away or not. An edit operation can be defined as deleting a character, adding a character, or replacing a character with another character.So, if the input is like S = "hello", T = "hallo", then the output will be True, as these two strings have edit distance of 1.To solve this, we will follow these steps −m := size of S, n := size of Ti := 0, j := 0count := 0if |m - n| > 1, thenreturn Falsewhile ... Read More

Advertisements