Found 10476 Articles for Python

Program to make all elements equal by performing given operation in Python

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

524 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

Program to perform given operation with each element of a list and given value in Python

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

358 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

Program to 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

Program to remove all nodes of a linked list whose value is same as in Python

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

413 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

Program to 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

Program to check whether a binary tree is BST or not in Python

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

661 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

Program to remove all nodes from BST which are not in range in Python

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

195 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

Program to check whether different brackets are balanced and well-formed or not in Python

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

275 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

Program to check whether parentheses are balanced or not in Python

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

266 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

Program to count number of unique binary search tree can be formed with 0 to n values in Python

Farhan Muhamed
Updated on 18-Aug-2025 11:28:58

243 Views

In this article, we will discuss a problem that involves counting the number of unique binary search tree (BSTs) that can be formed with a given number of nodes. We will explain the problem, testcases, its solution, and provide a Python implementation. Number of Unique Binary Search Trees For n Nodes In this problem, we are given an integer n, which represents the number of nodes in a binary search tree. The task is to find the number of different BSTs that can be formed using these n nodes and return the count. To understand the problem better, let's ... Read More

Advertisements