Find Product of Three Unique Elements in Python

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

604 Views

Suppose we have three numbers, x, y, and z, we have to find their product, but if any two numbers are equal, they do not count.So, if the input is like x = 5, y = 4, z = 2, then the output will be 40, as all three numbers are distinct so their product is 5 * 4 * 2 = 40To solve this, we will follow these steps −temp_set := a new setremove := a new setfor each i in [x, y, z], doif i is in temp_set, theninsert i into the set called removeinsert i in to ... Read More

Find Final Text in Editor by Typing and Backspacing in Python

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

167 Views

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

Find Number of Tasks That Can Be Finished with Given Conditions in Python

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

357 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

Minimum Number of Operations to Convert One Number to Another in Python

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

407 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

Find Two Numbers in a List That Sum Up to K in Python

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

837 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

Find the Sum of All Digits of a Given Number in Python

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

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

Minimum Operations to Make One String Substring of Another in Python

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

684 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

Find Nth Sequence Following String Rules in Python

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

314 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

Check 1-to-1 String Mapping in Python

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

496 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

Check String Conversion by Shifting Characters Clockwise in Python

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

427 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

Advertisements