Found 10783 Articles for Python

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

105 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

119 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

580 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

168 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

489 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

190 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

1K+ 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

Check if a number is Primorial Prime or not in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:41:55

660 Views

Suppose we have a number n, we have to check whether n is a primorial prime or not. A number is said to be a primorial prime when it is a prime number of the form pN# + 1 or pN# – 1 , where pN# indicates the primorial of pN such that the product of first N prime numbers.So, if the input is like 29, then the output will be True as 29 is Primorial prime of the form pN - 1 if N=3, Primorial is 2*3*5 = 30 and 30-1 = 29.To solve this, we will follow these ... Read More

Check if a number is an Achilles number or not in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:38:56

170 Views

Suppose we have a number n; we have to check whether n is an Achilles number or not. As we know a number is Achilles number when a number that is powerful (A number N is called Powerful Number when for every prime factor p of it, p^2 also divides it) but not a perfect power. Some of the examples of Achilles numbers are: 72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, 968, 972, 1125.So, if the input is like 108, then the output will be True, as 6 and 36 both divide it and it is ... Read More

Check if a number is a Trojan Numbers in Python

Arnab Chakraborty
Updated on 27-Aug-2020 13:35:01

93 Views

Suppose we have a number n, we have to check whether n is a Trojan Number or not. As we know that the Trojan Number is a number that is a strong number without a perfect power. A number n is a strong number when for every prime divisor or factor p of n, p^2 is also a divisor. In other words, every prime factor appears at least twice. Trojan numbers are strong. But the reverse is not true. So it means, not all strong numbers are Trojan numbers: only those that cannot be represented as a^b.So, if the input ... Read More

Advertisements