Found 10784 Articles for Python

Program to find final text in an editor by performing typing and backspacing in Python

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

133 Views

Suppose we have a string s that represents characters that typed into an editor, the symbol "

Program to find number of tasks can be finished with given conditions in Python

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

264 Views

Suppose we have a list of tasks and another list of people. The tasks[i] determines the amount of strength required to perform the ith task. And the people[i] determines the amount of strength the ith person has. Finally, we have to find the number of tasks that can be finished if one person can perform at most one task.So, if the input is like tasks = [4, 3, 9, 15], people = [10, 5, 3, 2], then the output will be 3, as the first person can perform task 9, second person can perform task 4, third person can perform ... Read More

Program to find minimum number of operations required to make one number to another in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:31:30

304 Views

Suppose we have a number start and another number end (start < end), we have to find the minimum number of operations required to convert start to end using these operations −Increment by 1Multiply by 2So, if the input is like start = 5, end = 11, then the output will be 2, as we can multiply 2 to get 10, then add 1 to get 11.To solve this, we will follow these steps −ct:= 0while end/2 >= start, doif end mod 2 is same as 1, thenend := end - 1end := end/2ct := ct + 2otherwise, end:= end/2ct ... Read More

Program to find any two numbers in a list that sums up to k in Python

Arnab Chakraborty
Updated on 05-Oct-2020 07:27:12

715 Views

Suppose we have a list of numbers called nums and we have another number k, we have to check whether any two numbers present in the list add up to k or now. Same elements must not be used twice. And numbers can be negative or 0.So, if the input is like nums = [45, 18, 9, 13, 12], k = 31, then the output will be True, as 18 + 13 = 31To solve this, we will follow these steps −temp_set:= a new setfor each num in nums, doif num is in temp_set, thenreturn Trueadd (k-num) into temp_setreturn FalseLet ... Read More

Program to find the sum of all digits of given number in Python

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

994 Views

Suppose we have a number num, we have to find the sum of its digits. We have to solve it without using strings.So, if the input is like num = 512, then the output will be 8, as 8 = 5 + 1 + 2.tput will be 8, as 8 = 5 + 1 + 2. To solve this, we will follow these steps −sum:= 0while num is not same as 0, dosum := sum + (num mod 10)num:= quotient of num/10return sumLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, num):   ... Read More

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

533 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

208 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

154 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

343 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

Advertisements