Found 10476 Articles for Python

Program to find minimum number of operations required to make one string substring of other in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:24:52

679 Views

Suppose we have two strings s and t, we have to find the minimum amount of operations required for s to make t a substring of s. Now, in each operation, we can choose any position in s and change the character at that position to any other character.So, if the input is like s = "abbpqr", t = "bbxy", then the output will be 2, as we can take the substring "bbpq" and change 'p' to 'x' and 'q' to 'y'.To solve this, we will follow these steps −k := size of t, n := size of sans := ... Read More

Program to find nth sequence after following the given string sequence rules in Python

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

310 Views

Suppose we have two strings s, t and another positive number n is also given, we have to find return the nth term of the sequence A where −A[0] = sA[1] = tA[n] = A[n - 1] + A[n - 2] when n is even, otherwise A[n] = A[n - 2] + A[n - 1].As an example, if s = "a" and t = "b", then the sequence A would be − ["a", "b", "ba" ("a" + "b"), "bba" ("b" + "ba"), "bbaba" ("bba" + "ba")]So, if the input is like s = "pk", t = "r", n = 4, ... Read More

Program to find the maximum width of a binary tree in Python

Arnab Chakraborty
Updated on 05-Oct-2020 11:54:32

185 Views

Suppose we have a binary tree, we have to find the maximum width of any level in the tree. Here the width of a level is the number of nodes that can hold between the leftmost node and the rightmost node.So, if the input is like then the output will be 2To solve this, we will follow these steps−create a map d, to hold minimum and maximum values, minimum is initially infinity and maximum is 0Define a function dfs() . This will take root, pos := 0, depth := 0if root is null, then o returnd[depth, 0] = minimum of d[depth, ... Read More

Program to check whether one string can be 1-to-1 mapped into another string in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:16:56

486 Views

Suppose we have two lowercase strings s, and t we have to check whether we can create one 1-to-1 mapping for each letter in s to another letter (may be the same letter) such that s can be mapped to t. (The ordering of characters will not be changed).So, if the input is like s = "papa", t = "lili", then the output will be True, as we can create this mapping: "p" to "l", "a" -> "i"To solve this, we will follow these steps −s_dict := a new mapt_dict := a new mapfor i in range 0 to minimum ... Read More

Program to check whether one value is present in BST or not in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:03:54

1K+ Views

Suppose we have a binary search tree and another input called val, we have to check whether val is present in the tree or not.So, if the input is likeval = 7, then the output will be True, as 7 is present in the tree.To solve this, we will follow these steps−Define a function solve() . This will take root, valif root is null, thenreturn Falseif data of root is same as val, thenreturn Trueif data of root < val, thenreturn solve(left of root, val)return solve(right of root, val)Let us see the following implementation to get better understanding− Live DemoExampleclass TreeNode: ... Read More

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

418 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

3K+ 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

2K+ 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

180 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

407 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

Advertisements