Found 10476 Articles for Python

Smallest Subsequence of Distinct Characters in Python

Arnab Chakraborty
Updated on 05-Mar-2020 07:02:11

200 Views

Suppose we have a text, we have to find the lexicographically smallest subsequence of text that contains all the distinct characters of text exactly once. So if the input is like “cdadabcc”, then the output will be “adbc”.To solve this, we will follow these steps −Define a stack st, two maps last_o and considered, they are initially blankfor i in range length of text – 1 down to 0if text[i] is not present in last_o −last_o[text[i]] := iconsidered[text[i]] := falsei := 0while i < length of textif stack has no elementspush text[i] into stackconsidered[text[i]] := trueincrease i by 1otherwise stack ... Read More

Insufficient Nodes in Root to Leaf Paths in Python

Arnab Chakraborty
Updated on 05-Mar-2020 06:54:00

279 Views

Suppose we have a binary tree. A node is known as insufficient if every such root to leaf path intersecting this node has sum strictly less than limit. We have to delete all insufficient nodes simultaneously, and return the root of the resulting binary tree. So if the tree is like, and the limit is 1 −Then the output tree will be −To solve this, we will follow these steps −Define a method solve(), this will take root and limitif node has no left and right subtree, thenif value of root is less than 1, return null, otherwise rootif root ... Read More

Grumpy Bookstore Owner in Python

Arnab Chakraborty
Updated on 02-May-2020 11:00:29

237 Views

Suppose a bookstore owner has a store open for number of customers list entries minutes. In every minute, some number of customers (customers[i]) enter the store, after that all those customers leave after the end of that minute. On some minutes, the owner is grumpy. Now if the owner is grumpy on the i-th minute, then grumpy[i] = 1, otherwise grumpy[i] = 0. When the bookstore owner is grumpy, the customers of that minute are unhappy, otherwise they are happy. The bookstore owner knows a technique to keep themselves not grumpy for X minutes straight. That technique cannot be used ... Read More

Capacity To Ship Packages Within D Days in Python

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

329 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

801 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

273 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

Akshitha Mote
Updated on 07-Jan-2025 11:15:58

2K+ Views

Shuffling an array involves rearranging the elements of the array into a random order. For example, if arr = [1, 2, 3, 4, 5], the output might be [5, 3, 2, 1, 4]. Let's explore different ways to shuffle an array in Python. Following are the various methods to shuffle an array in Python − Using shuffle() method Using permutation() method Using simple() method Shuffling using random indices Fisher-Yates shuffle Algorithm Using shuffle() Method The shuffle() method in ... Read More

Basic Calculator II in Python

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

915 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

744 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

968 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

Advertisements