Check If Robot Can Reach Target Position in Python

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

447 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

Check Reachability of Leftmost or Rightmost Position in Python

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

371 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

Calculate Years to Reach Target Amount in Python

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

228 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

Reverse Position of Each Word in a Given String in Python

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

654 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

Check Reverse Sublist to Form Another List in Python

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

226 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

Check if String is Repeating in Python

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

162 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

Count K-Length Substring Occurring More Than Once in Python

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

219 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

Find Sum of Digits Until 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

Check String Conversion by Removing One Element in Python

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

173 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

Find Duplicate Elements and Delete Last Occurrence in Python

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

352 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