Replace Each Element by Smallest Term at Left Side in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:19:25

249 Views

Suppose we have a list of numbers called nums, we have to replace every nums[i] with the smallest element left of i. We have to replace nums[0] with 0.So, if the input is like [15, 7, 9, 16, 12, 25], then the output will be [0, 15, 7, 7, 7, 7]To solve this, we will follow these steps −if nums is empty, thenreturn a new listj:= nums[0]nums[0]:= 0for i in range 1 to size of nums - 1, dok:= nums[i]nums[i]:= jj:= minimum of j, kreturn numsLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def ... Read More

Make All Elements Equal by Performing Given Operation in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:17:37

526 Views

Suppose we have given a list of numbers called nums, we want to make the values equal. Now let an operation where we pick one element from the list and increment every other value. We have to find the minimum number of operations required to make element values equal.So, if the input is like [2, 4, 5], then the output will be 5.To solve this, we will follow these steps −min_val := minimum of numss := 0for each num in nums, dos := s + (num - min_val)return sLet us see the following implementation to get better understanding −Example Live Democlass ... Read More

Perform Operations on List Elements in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:16:14

362 Views

Suppose we have a list of numbers called nums, we also have another string op representing operator like "+", "-", "/", or "*", and another value val is also given, we have to perform the operation on every number in nums with val and return the result.So, if the input is like [5, 3, 8], then the output will be [15, 9, 24]To solve this, we will follow these steps −res:= a new listfor each i in nums, doif op is same as '+', theninsert i+val at the end of resotherwise when op is same as '-', theninsert i-val at ... Read More

Convert Linked List Representing Binary Number to Decimal Integer in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:14:23

2K+ Views

Suppose we have a singly linked list. the linked list is representing a binary number with most significant digits first, we have to return it as decimal number.So, if the input is like [1, 0, 1, 1, 0], then the output will be 22To solve this, we will follow these steps −l := a new listwhile node is not null, doinsert value of node at the end of lnode:= next of nodek := 0, v:= 0for i in range size of l - 1 to 0, decrease by 1, doif l[i] is same as 1, thenv := v + 2^kk ... Read More

Remove All Nodes with Same Value from Linked List in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:08:18

416 Views

Suppose we have a singly linked list, and one target, we have to return the same linked after deleting all nodes whose value is same as target.So, if the input is like [5, 8, 2, 6, 5, 2, 9, 6, 2, 4], then the output will be [5, 8, 6, 5, 9, 6, 4, ]To solve this, we will follow these steps −head := nodewhile node and node.next are not null, dowhile value of next of node is same as target, donext of node := next of next of nodenode := next of nodeif value of head is same as ... Read More

Find Lexicographically Smallest String with One Swap in Python

Arnab Chakraborty
Updated on 06-Oct-2020 06:06:36

3K+ Views

Suppose we have a string s, we have to find the lexicographically smallest string that can be made if we can make at most one swap between two characters in the given string s.So, if the input is like "zyzx", then the output will be "xyzz"To solve this, we will follow these steps −temp := an array of size s and fill with 0m:= size of s - 1for i in range size of s -1 to -1, decrease by 1, doif s[i] < s[m], thenm := itemp[i] := mfor i in range 0 to size of s, doa := ... Read More

Check if a Binary Tree is a BST in Python

Arnab Chakraborty
Updated on 06-Oct-2020 05:59:22

665 Views

Suppose we have binary tree; we have to check whether it is a binary search tree or not. As we know a BST has following properties −all nodes on its left subtree is smaller than current node valueall nodes on its right subtree is larger than current node valuethese properties hold recursively for all nodesSo, if the input is likethen the output will be TrueTo solve this, we will follow these steps −x := a list of inorder traversal sequence of tree elementsif x is sorted, thenreturn truereturn falseLet us see the following implementation to get better understanding −Example Live Democlass ... Read More

Remove Nodes from BST Not in Range in Python

Arnab Chakraborty
Updated on 06-Oct-2020 05:56:14

197 Views

Suppose we have a BST, an two values low and high, we have to delete all nodes that are not between [low, high] (inclusive).So, if the input is likelow = 7 high = 10, then the output will beTo solve this, we will follow these steps −Define a function solve() . This will take root, low, highif root is null, thenreturnif low > data of root, thenreturn solve(right of root, low, high)if high < data of root, thenreturn solve(left of root, low, high)right of root := solve(right of root, low, high)left of root := solve(left of root, low, high)return rootLet ... Read More

Check Balanced and Well-Formed Brackets in Python

Arnab Chakraborty
Updated on 06-Oct-2020 05:53:03

277 Views

Suppose we have a string of brackets (round, curly, and square), we have to check whether the brackets are balanced (well-formed) or not.So, if the input is like s = "([()()]{[]})()", then the output will be TrueTo solve this, we will follow these steps −stack := a new listd := a hash map with key-value pairs ('}', '{'), (')', '('), (']', '[')for each character c in s, doif c is any one of '}])', thenif stack is empty or top of stack is not same as d[c], thenreturn Falsepop from stackotherwise, push c into stackreturn true when stack is empty, ... Read More

Check Whether Parentheses are Balanced in Python

Arnab Chakraborty
Updated on 06-Oct-2020 05:49:35

268 Views

Suppose we have a string s consisting of parenthesis "(" and ")". We have to check whether the parentheses are balanced or not.So, if the input is like s = "(()())(())", then the output will be TrueTo solve this, we will follow these steps −num_open := 0for each character c in s, doif c is same as ')', thenif num_open < 0, thennum_open := num_open - 1otherwise, return Falseotherwise, num_open := num_open + 1return inverse of num_openLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s):       num_open = 0   ... Read More

Advertisements