Found 10784 Articles for Python

Program to check one string can be converted to other by shifting characters clockwise in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:14:14

334 Views

Suppose we have two strings p and q, and also have a number r, we have to check whether p can be converted to q by shifting some characters clockwise at most r times. So, as an example, "c" can be turned into "e" using 2 clockwise shifts.So, if the input is like p = "abc", q = "ccc", r = 3, then the output will be True, as we can make "a" into "c" by using 2 clockwise shifts and then convert "b" into "c" by using 1 clockwise shift, for a total of 3 shifts.To solve this, we ... Read More

Program to add two numbers represented as strings in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:56:03

2K+ Views

Suppose we have two strings S, and T, these two are representing an integer, 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 = 262350179125To solve this, we will follow these steps −convert S and T from string to integerret = S + Treturn ret as stringLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, a, b):       return str(int(a) + int(b)) ob = Solution() print(ob.solve("256478921657", "5871257468"))Input"256478921657", "5871257468"Output262350179125

Program to check whether list is strictly increasing or strictly decreasing in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:53:08

1K+ Views

Suppose we have a list of numbers; we have to check whether the list is strictly increasing or strictly decreasing.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.To solve this, we will follow these steps −if size of nums

Program to sqeeze list elements from left or right to make it single element in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:48:02

133 Views

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] ]To solve this, we will follow these steps −ret := a list with only one element numswhile size of nums > 1, doif size of nums is same as 2, thennums ... Read More

Program to sorting important mails from different mailboxes in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:42:54

337 Views

Suppose we have a list of mailboxes. Here in each mailbox a list of strings is given, here each string is either "J" for junk, "P" for personal, "W" for Work. We will go through each mailbox in round robin order starting from the first mailbox, filtering out J, to form a single list and return the list.So, if the input is like mailboxes = [["W", "P"], ["J", "P", "J"], ["W"]], then the output will be ["W", "W", "P", "P"], as in order and without filtering, we have W -> J -> W -> P -> P -> J, now ... Read More

Program to count number of elements are placed at correct position in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:39:45

324 Views

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]To solve this, we will follow these steps −s := sort the list numscount := 0for i in range 0 to size of nums, doif s[i] is same as nums[i], thencount := count ... Read More

Program to check whether we can stand at least k distance away from the closest contacts in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:38:07

44 Views

Suppose we have a string s and a number k. Now each character in the string is either dot ('.') or 'x', where dot indicates an empty space and 'x' indicates a person. We have to check whether it's possible to choose a position to stand on such that the distance between us and the closest person to us is at least k. (Here the distance between each neighbouring indices is 1).So, if the input is like s = "x...x..", k = 2, then the output will be True, as we can stand at s[2] or s[6].To solve this, we ... Read More

Program to find shortest sublist so after sorting that entire list will be sorted in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:35:44

155 Views

Suppose we have a list of numbers called nums, we have to find the length of the shortest sublist in num, if the sublist is sorted, then the entire array nums will be sorted in ascending order.So, if the input is like nums = [1, 2, 5, 4, 9, 10], then the output will be 2, as Sorting the sublist [4, 3] would get us [0, 1, 3, 4, 8, 9]To solve this, we will follow these steps −f:= -1, l:= -1lst:= sort the list numsfor i in range 0 to size of nums, doif nums[i] is not same as ... Read More

Program to find shortest string after removing different adjacent bits in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:33:53

216 Views

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.To solve this, we will follow these steps −stack := a new listfor each c in s, ... Read More

Program to encode a string in normal form to its run-length form in Python

Arnab Chakraborty
Updated on 05-Oct-2020 06:31:52

328 Views

Suppose we have a string s. We have to encode this by using run-length encoding technique. 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.So, if the input is like s = "BBBBAAADDCBB", then the output will be "4B3A2D1C2B"To solve this, we will follow these steps −res := blank stringtmp := first character of scount := 1for i in range 1 to size of s, doif s[i] is not same as tmp, thenres := res concatenate count concatenate ... Read More

Advertisements