Found 10784 Articles for Python

Capacity To Ship Packages Within D Days in Python

Arnab Chakraborty
Updated on 02-May-2020 10:46:47

234 Views

Suppose there is a conveyor belt has packages that will be shipped from one port to another within D days. Here the i-th package on the conveyor belt has a weight of weights[i]. Each day, we will load the ship with packages on the belt. We will not load more weight than the maximum weight capacity of the ship. We have to find the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within D days. So if the input is like [3, 2, 2, 4, 1, 4] and D ... Read More

Construct Binary Search Tree from Preorder Traversal in Python

Arnab Chakraborty
Updated on 02-May-2020 10:45:57

635 Views

Suppose we have to create a binary search tree that matches the given preorder traversal. So if the pre-order traversal is like [8, 5, 1, 7, 10, 12], then the output will be [8, 5, 10, 1, 7, null, 12], so the tree will be −To solve this, we will follow these steps −root := 0th node of the preorder traversal liststack := a stack, and push root into the stackfor each element i from the second element of the preorder listi := a node with value iif value of i < top of the stack top element, thenleft of ... Read More

Smallest String Starting From Leaf in Python

Arnab Chakraborty
Updated on 02-May-2020 10:35:23

191 Views

Suppose we have the root of a binary tree, each node is containing a value from 0 to 25 , which are representing the letters 'a' to 'z': a value of 0 represents 'a', a value of 1 represents 'b', and so on. We have to search the lexicographically smallest string that starts at a leaf of this tree and finishes at the root. So if the tree is like −Then the output will be “adz” as the sequence is [0, 3, 25]To solve this, we will follow these steps −Define a dfs traversal method as followsif node is not ... Read More

Shuffle an Array in Python

Arnab Chakraborty
Updated on 30-Apr-2020 09:50:15

1K+ Views

Suppose we have an array A, we have to shuffle a set of numbers without duplicates. So if the input is like [1, 2, 3], then for shuffling, it will be [1, 3, 2], after resetting, if we shuffle again, it will be [2, 3, 1]To solve this, we will follow these steps −There will be different methods. these are init(), reset(), shuffle(). These will act like below −init will be like −original := a copy of the given arraytemp := numsindices := a list of numbers from 0 to length of nums – 1the reset() will return the original ... Read More

Basic Calculator II in Python

Arnab Chakraborty
Updated on 04-Mar-2020 06:20:53

630 Views

Suppose we have to implement a basic calculator to evaluate a simple expression string. The expression string will hold only non-negative integers, some operators like, +, -, *, / and empty spaces. The integer division should take the quotient part only.So if the input is like “3+2*2”, then the output will be 7.To solve this, we will follow these steps −define a stack s, i := 0, x := an empty stringfor each character j in sif j is not a blank characterappend j into xs := x, n := length of xwhile i < nif s[i] is ‘/’, thenincrease ... Read More

Binary Tree Zigzag Level Order Traversal in Python

Arnab Chakraborty
Updated on 30-Apr-2020 09:13:33

588 Views

Suppose we have a binary tree; we have to find the Zigzag level order traversal. So for the first row, scan from left to right, then right to left from the second row, then again left to right and so on. So if the tree is like −The traversal sequence will be [[3], [20, 9], [15, 7]]To solve this, we will follow these steps −if the tree is empty, return empty listqueue := make a queue and insert root into the queue, create two empty lists res and res2, and set flag as Truewhile queue is not emptymake a list ... Read More

Convert list of string to list of list in Python

Pradeep Elance
Updated on 03-Mar-2020 06:34:31

736 Views

In this article we will see how to create a list of lists which contain string data types. The inner list themselves or of string data type and they may contain numeric or strings as their elements.Using strip and splitWe use these two methods which will first separate out the lists and then convert each element of the list to a string.Example Live Demolist1 = [ '[0, 1, 2, 3]', '["Mon", "Tue", "Wed", "Thu"]' ] print ("The given list is : " + str(list1)) print("") # using strip() + split() result = [k.strip("[]").split(", ") for k in list1] print ("Converting list ... Read More

Python - cmp() Method

Pradeep Elance
Updated on 03-Mar-2020 06:30:00

3K+ Views

The cmp() is part of the python standard library which compares two integers. The result of comparison is -1 if the first integer is smaller than second and 1 if the first integer is greater than the second. If both are equal the result of cmp() is zero.Below example illustrates different scenario showing the use of cmp() method.Example Live Demodef cmp(x, y):    return (x > y) - (x < y) #x>y x = 5 y = 3 print("The cmp value for x>y is : ",cmp(x, y),"") #xy is : 1 The cmp value for x

Python - Clearing list as dictionary value

Pradeep Elance
Updated on 03-Mar-2020 06:27:16

100 Views

In this article we consider a dictionary where the values are presented as lists. Then we consider clearing those values from the lists. We have two approaches here. One is to use the clear methods and another is to designate empty values to each key using list comprehension.Example Live Demox1 = {"Apple" : [4, 6, 9, 2], "Grape" : [7, 8, 2, 1], "Orange" : [3, 6, 2, 4]} x2 = {"mango" : [4, 6, 9, 2], "pineapple" : [7, 8, 2, 1], "cherry" : [3, 6, 2, 4]} print("The given input is : " + str(x1)) # using loop + ... Read More

Python - Check if frequencies of all characters of a string are different

Pradeep Elance
Updated on 03-Mar-2020 06:21:19

143 Views

In this article we will see how to find the frequency of each character in a given string. Then see if two or more characters have the same frequency in the given string or not. We will accomplish this in two steps. In the first program we will just find out the frequency of each character.Frequency of each characterHere we find the frequency of each character in the given input screen. We declare a empty dictionary and then add each character as a string. We also assign keys to each of the character to create the key-value pair needed by ... Read More

Advertisements