- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 10784 Articles for Python
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Pradeep Elance](https://www.tutorialspoint.com/assets/profiles/121076/profile/60_44312-1565945237.jpg)
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
![Pradeep Elance](https://www.tutorialspoint.com/assets/profiles/121076/profile/60_44312-1565945237.jpg)
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
![Pradeep Elance](https://www.tutorialspoint.com/assets/profiles/121076/profile/60_44312-1565945237.jpg)
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
![Pradeep Elance](https://www.tutorialspoint.com/assets/profiles/121076/profile/60_44312-1565945237.jpg)
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