- 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)
303 Views
Suppose we have mapping like 'a' = 1, 'b' = 2, ... 'z' = 26, and we have an encoded message message string, we have to count the number of ways it can be decoded.So, if the input is like message = "222", then the output will be 3, as This can be decoded 3 ways: bbb, bv, and vb.To solve this, we will follow these steps −memo := a list of 0s of size same as message size + 1memo[0] := 1memo[1] := 1 when message[0] is not same as "0" otherwise 0for i in range 2 to size ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
246 Views
Suppose we have two strings S and T of same length, we have to check whether it is possible to cut both strings at a common point so that the first part of S and the second part of T form a palindrome.So, if the input is like S = "cat" T = "pac", then the output will be True, as If we cut the strings into "c" + "at" and "d" + "ac", then "c" + "ac" is a palindrome.To solve this, we will follow these steps −n := size of ai := 0while i < n and a[i] ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
114 Views
Suppose we have a 2D matrix where matrix[i] represents the list of prerequisite courses needed to enroll course i. Now, we have to check whether it is possible to take all courses or not.So, if the input is like matrix = [[1], [2], []], then the output will be True, as we can take course 2, then course 1, and then course 0.To solve this, we will follow these steps −Define a function dfs(). This will take iif vis[i] is true, thenreturn falseif chk[i] is true, thenreturn Truevis[i]:= Truefor each j in matrix[i], doif dfs(j) is false, thenreturn Falsevis[i]:= Falsechk[i]:= ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
110 Views
Suppose we have a list of numbers called nums, we have to find the number of sublists where the first element and the last element are same.So, if the input is like nums = [10, 15, 13, 10], then the output will be 5, as the sublists with same first and last element are: [10], [15], [13], [10], [10, 15, 13, 10].To solve this, we will follow these steps −num_sublists := size of numsd := an empty mapfor each n in nums, dod[n] := d[n] + 1for each number k and corresponding frequency v of elements in d, doif v ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
202 Views
Suppose we have a list of numbers called nums of length n + 1. These numbers are picked from range 1, 2, ..., n. As we know, using the pigeonhole principle, there must be a duplicate. We have to find that and return it.So, if the input is like [2, 1, 4, 3, 3], then the output will be 3To solve this, we will follow these steps −l := size of numstemp := l*(l-1) /2temp_sum := sum of all elements in numsreturn (temp_sum - temp)Let us see the following implementation to get better understanding −Example Live Democlass Solution: def solve(self, ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
301 Views
Suppose we have a list of words in lowercase letters, we have to find the length of the longest contiguous sublist where all words have the same first letter.So, if the input is like ["she", "sells", "seashells", "on", "the", "seashore"], then the output will be 3 as three contiguous words are "she", "sells", "seashells", all have same first letter 's'.To solve this, we will follow these steps −maxlength := 0curr_letter := Null, curr_length := 0for each word in words, doif curr_letter is null or curr_letter is not same as word[0], thenmaxlength := maximum of maxlength, curr_lengthcurr_letter := word[0], curr_length := ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
67 Views
Suppose we have a number n, this is representing programmers looking to enter a convention, and we also have a list of number, convention 1 represents a programmer and 0 represents empty space. Now the condition is no two programmers can sit next to each other, we have to check whether all n programmers can enter the convention or not.So, if the input is like n = 2, convention = [0, 0, 1, 0, 0, 0, 1], then the output will be TrueTo solve this, we will follow these steps −for i in range 0 to size of conv, doa:= ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
314 Views
Suppose we have a positive number n, where n is representing the amount of cents we have, we have to find the formatted currency amount.So, if the input is like n = 123456, then the output will be "1, 234.56".To solve this, we will follow these steps −cents := n as stringif size of cents < 2, thenreturn '0.0' concatenate centsif size of cents is same as 2, thenreturn '0.' concatenate centscurrency := substring of cents except last two digitscents := '.' concatenate last two digitwhile size of currency > 3, docents := ', ' concatenate last three digit of ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
498 Views
Suppose we have a string s, representing a password, we have to check the password criteria. There are few rules, that we have to follow −Password length will be At least 8 characters and at most 20 characters long.Password contains at least one digitPassword contains at least one lowercase character and one uppercase characterPassword contains at least one special character like !"#$%&\'()*+, -./:;?@[\]^_`{|}~Password does not contain any other character like tabs or new lines.So, if the input is like "@bCd12#4", then the output will be True.To solve this, we will follow these steps −a:= 0, b:= 0, c:= 0, d:= ... Read More
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
742 Views
Suppose we have a number n, we have to find the nth (0-indexed) row of Pascal's triangle. As we know the Pascal's triangle can be created as follows −In the top row, there is an array of 1.Subsequent row is made by adding the number above and to the left with the number above and to the right.So few rows are as follows −So, if the input is like 4, then the output will be [1, 4, 6, 4, 1]To solve this, we will follow these steps −if n is same as 0, thenreturn [1]if n is same as 1, ... Read More