Found 10476 Articles for Python

Program to check robot can reach target position or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:47:26

441 Views

Suppose we have a robot, that is currently sitting in at position (0, 0) (Cartesian plane). If we have list of its moves that it can make, containing N(North), S(South), W(West), and E(East). We have to check whether it can reach at destination coordinate (x, y).So, if the input is like moves = ['N', 'N', 'E', 'E', 'S'], (x, y) = (2, 1), then the output will be True, To solve this, we will follow these steps −temp_coord := [0, 0]for each move in moves, doif move is same as "N", thentemp_coord[1] := temp_coord[1] + 1otherwise when move is same ... Read More

Program to check we can reach leftmost or rightmost position or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:44:57

370 Views

Suppose we have a string containing letters of three types, R, B, and dot(.). Here R stands for our current position, B stands for a blocked position, and dot(.) stands for an empty position. Now, in one step, we can move to any adjacent position to our current position, as long as it is valid (empty). We have to check whether we can reach either the leftmost position or the rightmost position.So, if the input is like s = "...........R.....BBBB.....", then the output will be True, as R can reach left most position, because there is no block.To solve this, ... Read More

Program to find how many years it will take to reach t amount in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:43:46

225 Views

Suppose we have some parameters P, O, E, T. If we have P dollars in principal that we want to invest the stock market. The stock market alternates between first returning E and then O percent interest per year, we have to check how many years it would take to reach at least T dollars.So, if the input is like P = 200, O = 10, E = 25, T = 300, then the output will be 3 as in the first year we will get interest 25%, so end up with 200+50 = 250, then next year we will ... Read More

Program to reverse the position of each word of a given string in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:42:28

651 Views

Suppose we have a string of words delimited by spaces; we have to reverse the order of words.So, if the input is like "Hello world, I love python programming", then the output will be "programming python love I world, Hello"To solve this, we will follow these steps −temp := make a list of words by splitting s using blank spacetemp := reverse the list tempreturn a string by joining the elements from temp using space delimiter.Let us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s):       temp = s.split(' ')   ... Read More

Program to check if we reverse sublist of one list to form second list or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:41:16

222 Views

Suppose we have two lists of numbers called A, and B. We have to take some sublist in A and reverse it. Then check whether it is possible to turn A into B or not. We can take sublist and reverse it any number of times.So, if the input is like A = [2, 3, 4, 9, 10], B = [4, 3, 2, 10, 9], then the output will be True as we can reverse [2, 3, 4] and [9, 10].To solve this, we will follow these steps −res := a map, initially emptyfor each n in nums, dores[n] := ... Read More

Program to check the string is repeating string or not in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:39:32

159 Views

Suppose we have a string, we have to check whether it's a repeating string or not.So, if the input is like string = "helloworldhelloworld", then the output will be TrueTo solve this, we will follow these steps −n := size of sDefine a function findFactors() . This will take nf := a new seti := 1while i * i

Program to count k length substring that occurs more than once in the given string in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:36:43

215 Views

Suppose we have a string s and a number k, we have to find the number of k-length substrings of s, that occur more than once in s.So, if the input is like s = "xxxyyy", k = 2, then the output will be 2To solve this, we will follow these steps −seen := a new listfor i in range 0 to size of s - k, dot := substring of s [from index i to i + k - 1]insert t at the end of seenmp := a map for all distinct element in seen and their occurrencesreturn sum ... Read More

Program to find sum of digits until it is one digit number in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:34:42

4K+ Views

Suppose we have a positive number n, we will add all of its digits to get a new number. Now repeat this operation until it is less than 10.So, if the input is like 9625, then the output will be 4.To solve this, we will follow these steps −Define a method solve(), this will take nif n < 10, thenreturn ns := 0l := the floor of (log(n) base 10 + 1)while l > 0, dos := s + (n mod 10)n := quotient of n / 10l := l - 1return solve(s)Let us see the following implementation to get ... Read More

Program to check one string can be converted to another by removing one element in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:33:00

171 Views

Suppose we have two strings s and t, we have to check whether we can get t by removing 1 letter from s.So, if the input is like s = "world", t = "wrld", then the output will be True.To solve this, we will follow these steps −i:= 0n:= size of swhile i < n, dotemp:= substring of s[from index 0 to i-1] concatenate substring of s[from index i+1 to end]if temp is same as t, thenreturn Truei := i + 1return FalseLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s, t): ... Read More

Program to find duplicate elements and delete last occurrence of them in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:31:41

349 Views

Suppose we have a list of numbers A, we have to find all duplicate numbers and remove their last occurrences.So, if the input is like [10, 30, 40, 10, 30, 50], then the output will be [10, 30, 40, 50]To solve this, we will follow these steps −seen:= a new mapd:= a new mapfor i in range 0 to size of nums, doif nums[i] is not in d, thend[nums[i]]:= 1otherwise, d[nums[i]] := d[nums[i]] + 1i:= 0while i < size of nums, don:= d[nums[i]]if nums[i] is not in seen, thenseen[nums[i]]:= 1otherwise, seen[nums[i]] := seen[nums[i]] + 1if n is same as seen[nums[i]] ... Read More

Advertisements