Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Articles
Page 638 of 855
Program to add two numbers represented as strings in Python
Suppose we have two strings S and T, these two are representing integers. We have to add them and find the result in the same string representation. So, if the input is like "256478921657", "5871257468", then the output will be "262350179125", as 256478921657 + 5871257468 = 262350179125. Approach To solve this, we will follow these steps − Convert S and T from string to integer Add the integers: result = S + T Return the result as string Example Let us see the following implementation to get better understanding − ...
Read MoreProgram to check whether list is strictly increasing or strictly decreasing in Python
Suppose we have a list of numbers; we have to check whether the list is strictly increasing or strictly decreasing. A list is strictly increasing if each element is larger than the previous one, and strictly decreasing if each element is smaller than the previous one. So, if the input is like nums = [10, 12, 23, 34, 55], then the output will be True, as all elements are distinct and each element is larger than the previous one, so this is strictly increasing. Algorithm To solve this, we will follow these steps ? If size of nums
Read MoreProgram to sqeeze list elements from left or right to make it single element in Python
Suppose we have a list of numbers called nums, we have to squeeze it from both the left and the right until there is one remaining element. We will return the states at each step. So, if the input is like nums = [10, 20, 30, 40, 50, 60], then the output will be ? [ [10, 20, 30, 40, 50, 60], [30, 30, 40, 110], [60, 150], [210] ] Algorithm To solve this, we will follow these steps ? ret := a list with only ...
Read MoreProgram to sorting important mails from different mailboxes in Python
Suppose we have a list of mailboxes where each mailbox contains a list of strings. Each string represents either "J" for junk, "P" for personal, or "W" for work emails. We need to process each mailbox in round-robin order starting from the first mailbox, filter out junk emails, and return a single sorted list. So, if the input is like mailboxes = [["W", "P"], ["J", "P", "J"], ["W"]], then the output will be ["W", "W", "P", "P"]. In processing order without filtering, we have W → J → W → P → P → J. After filtering out junk ...
Read MoreProgram to count number of elements are placed at correct position in Python
Suppose we have a list of numbers called nums, we have to find the number of elements that are present in the correct indices, when the list was to be sorted. So, if the input is like [2, 8, 4, 5, 11], then the output will be 2, as the elements 2 and 11 are in their correct positions. The sorted sequence will be [2, 4, 5, 8, 11] Algorithm To solve this, we will follow these steps − s := sort the list nums count := 0 for i in range 0 to size ...
Read MoreProgram to check whether we can stand at least k distance away from the closest contacts in Python
Suppose we have a string s and a number k. Each character in the string is either a dot ('.') or 'x', where dot indicates an empty space and 'x' indicates a person. We need to check whether it's possible to choose a position to stand on such that the distance between us and the closest person is at least k units away. The distance between neighboring indices is 1. For example, if the input is s = "x...x.." and k = 2, the output will be True because we can stand at position s[2] or s[6]. Algorithm ...
Read MoreProgram to find shortest sublist so after sorting that entire list will be sorted in Python
Sometimes we need to find the shortest sublist that, when sorted, makes the entire list sorted. This problem involves comparing the original list with its sorted version to identify the boundaries of the unsorted region. So, if the input is like nums = [1, 2, 5, 4, 9, 10], then the output will be 2, as sorting the sublist [5, 4] would make the entire list sorted: [1, 2, 4, 5, 9, 10]. Algorithm To solve this, we will follow these steps − Initialize first := -1, last := -1 to track the boundaries Create ...
Read MoreProgram to find shortest string after removing different adjacent bits in Python
Suppose we have a binary string s, we can delete any two adjacent letters if they are different. Finally, we have to find the length of the smallest string that we can get if we are able to perform this operation as many times as we want. So, if the input is like s = "1100011", then the output will be 1, as after deleting "10" we get "10011", then again delete "10", it will be "011", then delete "01", it will have left "1". Algorithm Approach To solve this, we will follow these steps − ...
Read MoreProgram to encode a string in normal form to its run-length form in Python
Run-length encoding is a simple compression technique that replaces consecutive identical characters with a count followed by the character. For example, "AAABBC" becomes "3A2B1C". So, if the input is like s = "BBBBAAADDCBB", then the output will be "4B3A2D1C2B". Algorithm To solve this, we will follow these steps − Initialize an empty result string Set tmp to the first character and count to 1 Iterate through the string starting from index 1 If current character differs from tmp, append ...
Read MoreProgram to decode a run-length form of string into normal form in Python
Suppose we have a string s. The s is a run-length encoded string, we have to find the decoded version of it. As we know, run-length encoding is a fast and simple method of encoding strings. The idea is as follows − The repeated successive elements (characters) as a single count and character. For example, if the string is like "BBBBAAADDCBB" would be encoded as "4B3A2D1C2B". So, if the input is like s = "4B3A2D1C2B", then the output will be "BBBBAAADDCBB" Algorithm To solve this, we will follow these steps ? ...
Read More