Found 10476 Articles for Python

Find an integer X which is divisor of all except exactly one element in an array in Python

Arnab Chakraborty
Updated on 28-Aug-2020 11:56:56

151 Views

Suppose we have an array of numbers; we have to find a number B which is the divisor of all except for exactly one element in the given array. We have to keep in mind that the GCD of all the elements is not 1.So, if the input is like {8, 16, 4, 24}, then the output will be 8 as this is the divisor of all except 4.To solve this, we will follow these steps −n := size of arrayif n is same as 1, thenreturn(array[0] + 1)prefix := an array of size n, and fill with 0suffix := ... Read More

Find a string in lexicographic order which is in between given two strings in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:43:02

271 Views

Suppose we have two strings S and T, we have to check whether a string of the same length which is lexicographically bigger than S and smaller than T. We have to return -1 if no such string is available. We have to keep in mind that S = S1S2… Sn is termed to be lexicographically less than T = T1T2… Tn, provided there exists an i, so that S1= T1, S2= T2, … Si – 1= Ti – 1, Si < Ti.So, if the input is like S = "bbb" and T = "ddd", then the output will be ... Read More

Find a number which give minimum sum when XOR with every number of array of integer in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:12:07

119 Views

Suppose we have an array A, we have to find a number X such that (A[0] XOR X) + (A[1] XOR X) + … + A[n – 1] XOR X is as minimum as possible.So, if the input is like [3, 4, 5, 6, 7], then the output will be X = 7 , Sum = 10To solve this, we will follow these steps −Define a function search_res() . This will take arr, nelement := arr[0]for i in range 0 to size of arr, doif arr[i] > element, thenelement := arr[i]p := integer of (log of element base 2) + ... Read More

Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes in Python

Arnab Chakraborty
Updated on 27-Aug-2020 14:25:51

255 Views

Suppose we have two sorted linked lists, we have to make a linked list that consists of largest sum path from start node to end node. The final list may consist of nodes from both input lists.When we are creating the result list, we may switch to the other input list only for the point of intersection (two node with the same value in the lists). We have to solve it using constant amount of extra space.So, if the input is like [6, 8, 35, 95, 115, 125], [5, 8, 17, 37, 95, 105, 125, 135], then the output will ... Read More

Construct a distinct elements array with given size, sum and element upper bound in Python

Arnab Chakraborty
Updated on 27-Aug-2020 14:14:11

179 Views

Suppose we have one size variable N, we also have one variable SUM this is the total sum of all elements available in the array and another variable K such that there is no element in array is greater than K, We have to find one orthogonal array where all elements in the array are distinct. If there is no solution return -1.So, if the input is like N = 4, SUM = 16 K = 9, then the output will be [1, 2, 4, 9]To solve this, we will follow these steps −minimum_sum := (N *(N + 1)) / ... Read More

Construct a DataFrame in Pandas using string data in Python

Arnab Chakraborty
Updated on 27-Aug-2020 14:11:58

647 Views

Here we will see how we can construct a pandas dataframe using string type data. Pandas supports csv files, but we can do the same using string also. For string type data, we have to use one wrapper, that helps to simulate as the data is taken as csv reader.Here we are using a string that takes data and separated by semicolon.ExampleLet us see the following implementation to get better understanding −import pandas as pd from io import StringIO str_data = StringIO("""Id;Subject;Course_Fee    10;DBMS;3000    11;Basic Maths;2000    12;Data Science;40000    13;Algorithm;5000    """) df = pd.read_csv(str_data, sep =";") print(df)OutputId ... Read More

Construct a BST from given postorder traversal using Stack in Python

Arnab Chakraborty
Updated on 27-Aug-2020 14:10:31

254 Views

Suppose we have one postorder traversal of a binary search tree; we have to find the binary search tree from it.So, if the input is like [6, 12, 10, 55, 45, 15], then the output willTo solve this, we will follow these steps −Define a function solve() . This will take postordern := size of postorderroot := make a new tree node with last element of postorderstk := a stackinsert root into stki := n - 2while i >= 0, dox := make a new node with value postorder[i]while stk is not empty and postorder[i] < value of top of ... Read More

Construct a Binary Tree from Postorder and Inorder in Python

Arnab Chakraborty
Updated on 27-Aug-2020 14:07:33

648 Views

Suppose we have the inorder and postorder traversal sequence of a binary tree. We have to generate the tree from these sequences. So if the postorder and inorder sequences are [9, 15, 7, 20, 3] and [9, 3, 15, 20, 7], then the tree will be −Let us see the steps −Define a method build_tree(), this will take inorder, postorder −If inorder list is not empty −root := make a tree node with the last value of postorder, then delete that elementind := index of root data in inorder listright of root := build_tree(inorder from index ind to end, postorder)left ... Read More

Construct a Binary Search Tree from given postorder in Python

Arnab Chakraborty
Updated on 27-Aug-2020 14:04:34

291 Views

Suppose we have the postorder traversal sequence of a binary search tree. We have to generate the tree from these sequences. So, if the postorder sequences is [9, 15, 7, 20, 3], then the tree will be −To form a tree we need inorder traversal also, but for binary search tree, the inorder traversal will be in the sorted form.Let us see the steps −Inorder = sorted list of postorder traversal.Define a method build_tree(), this will take inorder, postorder −If inorder list is not empty −root := make a tree node with the last value of postorder, then delete that ... Read More

Heap in C++ STL - make_heap(), push_heap(), pop_heap(), sort_heap(), is_heap, is_heap_until()

Arnab Chakraborty
Updated on 27-Aug-2020 13:49:11

2K+ Views

In this section we will see the heap data structure present in C++ STL. This permits faster input into heap and retrieval of a number always results in the largest number i.e. largest number of the remaining numbers is popped out each time. Other elements of the heap are arranged which depends on the implementation. The heap operations are as follows −make_heap() − This converts a range in a container to a heap.front() − This returns first element of heap which is the largest number.ExampleLet us see the following implementation to get better understanding − Live Demo#include using namespace std; int ... Read More

Advertisements