Found 10476 Articles for Python

Program to find out if the strings supplied differ by a character in the same position in Python

Arnab Chakraborty
Updated on 18-May-2021 12:12:02

180 Views

Suppose, we are provided an array that contains several strings that are of the same length. We have to find out if any two of the supplied strings differ by a single character at the same position. If this difference is present, we return true or else we return false.So, if the input is like dict = ['pqrs', 'prqs', 'paqs'], then the output will be True. The output produced is True because the strings listed in the input all differ in index 1. So, if any two pairs are taken, there is a difference in the same position.To solve this, ... Read More

Program to find out the index of the most frequent element in a concealed array in Python

Arnab Chakraborty
Updated on 18-May-2021 12:08:02

257 Views

Suppose, we are given a class called 'TestArray' that contains an private array which can only contain values 0 or 1; and two public member functions length() and query(). The function length() returns the length of the array and the function query() returns three different values comparing various values in the array. The function takes four values p, q, r, s as input and works like this −if all the four values in the given indexes of the array are either 0 or 1, it returns 4.else if any three values in the given indexes of the array are the ... Read More

Program to find out the index in an array where the largest element is situated in Python

Arnab Chakraborty
Updated on 18-May-2021 12:06:41

204 Views

Suppose, we are given a class called 'TestArray' that contains an array that is not accessible by other classes and two public member functions length() and compare(). The function length() returns the length of the array and the function compare() returns three different values comparing various subarrays from the array. The function takes four values l, r, x, y as input and works like this −if (array[l] + array[l+1]+......+array[r-1]+array[r]) > (array[x] + array[x+1]+......+array[y1]+array[y]); it returns 1if (array[l] + array[l+1]+......+array[r-1]+array[r]) = (array[x] + array[x+1]+......+array[y1]+array[y]); it returns 0if (array[l] + array[l+1]+......+array[r-1]+array[r]) < (array[x] + array[x+1]+......+array[y1]+array[y]); it returns -1We have to find out ... Read More

Program to find the diameter of a n-ary tree in Python

Arnab Chakraborty
Updated on 18-May-2021 12:05:16

400 Views

Suppose, we are given an n-ary tree and said to determine the diameter of the tree. The diameter of the tree is the longest path that is present between any two leaf nodes of the tree. We have to find out and return the integer value that represents the diameter of the tree.So, if the input is likethen the output will be 3.The diameter of this n-ary tree consists of the edges 27->14, 14->42, and 42->56 or 42->65 (marked in the diagram by red lines). The path length is 3.To solve this, we will follow these steps −ans := 1Define ... Read More

Program to find the root of a n-ary tree in Python

Arnab Chakraborty
Updated on 18-May-2021 12:04:53

623 Views

Suppose, we are given the nodes of an n-ary tree in an array. We have to find and return the root node of the tree by reconstructing it. The full tree has to be displayed from the returned node in preorder notation.So, if the input is likethen the output will be[14, 27, 32, 42, 56, 65]We will use the root of the tree to display the pre order traversal of the tree. So, the output is a pre order traversal of the tree.To solve this, we will follow these steps −indegree := a new map containing integer valuesfor each node ... Read More

Program to make a copy of a n-ary tree in Python

Arnab Chakraborty
Updated on 18-May-2021 12:01:45

2K+ Views

Suppose, we have been provided a n-ary tree whose root is given to us 'root'. We have to make a copy of the full n-ary binary tree and perform a preorder traversal of both trees. The copied tree has to be stored using another root node. The node structure of the tree is given below −Node:    value :    children : So, if the input is like, then the output will be[14, 27, 32, 42, 56, 65]The preorder representation of the input tree and the output tree will be the same as an exact copy of the tree ... Read More

Program to find minimum changes required for alternating binary string in Python

Arnab Chakraborty
Updated on 18-May-2021 11:59:02

350 Views

Suppose we have a binary string s. Let us consider an operation where we can flip one bit. The string s is called alternating string if no two adjacent characters are same. We have to find the minimum number of operations needed to make s alternating.So, if the input is like s = "11100011", then the output will be 3 because if we flip bits at position 1, 4 and 7, then, it will be "10101010", then all are alternating.To solve this, we will follow these steps −change := 0even_1 := 0, even_0 := 0odd_1 := 0, odd_0 := 0for ... Read More

Program to check whether an array Is sorted and rotated in Python

Arnab Chakraborty
Updated on 18-May-2021 11:57:26

557 Views

Suppose we have an array called nums, we have to check whether the array was originally sorted in non-decreasing order, and then rotated some number of positions (may be zero) or not. Duplicates may also present in the array.So, if the input is like nums = [12, 15, 2, 5, 6, 9], then the output will be True, because it is rotated to the right for two placesTo solve this, we will follow these steps −j := 0while j < size of nums - 1 and nums[j] res[i + 1], thenreturn Falsereturn TrueExample (Python)Let us see the following implementation ... Read More

Program to find sum of unique elements in Python

Arnab Chakraborty
Updated on 18-May-2021 11:56:41

2K+ Views

Suppose we have an array nums with few duplicate elements and some unique elements. We have to find the sum of all the unique elements present in nums.So, if the input is like nums = [5, 2, 1, 5, 3, 1, 3, 8], then the output will be 10 because only unique elements are 8 and 2, so their sum is 10.To solve this, we will follow these steps −count := a dictionary holding all unique elements and their frequencyans := 0for each index i and value v in nums, doif count[v] is same as 1, thenans := ans + ... Read More

Program to find maximum number of balls in a box using Python

Arnab Chakraborty
Updated on 18-May-2021 11:56:18

2K+ Views

Suppose we have a ball factory where we have n balls numbered from l to r (both inclusive) and have an infinite number of boxes numbered from 1 to infinity. So if we put each ball in the box with a number same to the sum of digits of the ball's number. (As an example, the ball number 123 will be put in the box number 1 + 2 + 3 = 6). So if we have two values l and r, we have to find the number of balls in the box with the most balls.So, if the input ... Read More

Advertisements