Suppose we have a number n, and p and q. Now suppose we are standing in a line of n people. We do not know which position we are in, but we know there are at least p people in front of us and at most q people behind us. We have to find the number of possible positions we could be in.So, if the input is like n = 10, p = 3, q = 4, then the output will be 5, as there are 10 people and at least 3 are in front and at most 4 are ... Read More
Suppose we have a lowercase alphabet string s, we have to find a string with all the vowels of s in sorted sequence followed by all consonants of s in sorted sequence.So, if the input is like "helloworld", then the output will be "eoodhlllrw", as vowels are "eo" And consonants are in sorted order "dhlllrw"To solve this, we will follow these steps −k := blank string, t := blank stringfor each character c in s, doif c is a vowel, thenk := k concatenate cotherwise, t := t concatenate creturn (k after sort and concatenate t after sorting)Let us see ... Read More
Suppose we have a lowercase alphabet string text, and have another string called key. We have to find a new string where every letter in text[i] is moved to the right side with offset key[i]. Here the offset represented by key[i]'s position in the alphabet (A=0, B=1 etc.) If the letter overflows, it gets wrapped around the other side.So, if the input is like text = "code", key = "team", then the output will be "vsdq"To solve this, we will follow these steps −cip := a new liststart := ASCII of 'a'for each l from text and k from key, ... Read More
Suppose we have a string s and a number n, we have to rearrange s into n rows so that s can be selected vertically (top to down, left to right).So, if the input is like s = "ilovepythonprogramming" n = 5, then the output will be ['ipnrn', 'lypag', 'otrm', 'vhom', 'eogi']To solve this, we will follow these steps −L := empty listfor i in range 0 to n - 1:insert a string by taking each nth character starting from i, and insert into Lreturn LLet us see the following implementation to get better understanding −Example Live Democlass Solution: def ... Read More
Suppose we have a Unix path, in a list of strings, we have to find its resolved version. As we know in Unix, ".." denotes the previous directory and "." denotes stay on the current directory. Here resolving indicates evaluation of the two symbols so that we get the final directory we're currently in.So, if the input is like path = ["usr", "..", "usr", ".", "local", "etc", "foo"], then the output will be ['usr', 'local', 'etc', 'foo'], as the part represents "/usr/../usr/./local/etc" which resolves to "/usr/local/etc/foo"To solve this, we will follow these steps −s := a new listfor each element ... Read More
Suppose we have a list of sorted numbers called nums we have to find the number of unique elements in the list.So, if the input is like nums = [3, 3, 3, 4, 5, 7, 7], then the output will be 4, as The unique numbers are [3, 4, 5, 7]To solve this, we will follow these steps −s:= a new setcnt:= 0for each i in nums, doif i is not in s, theninsert i into scnt := cnt + 1return cntLet us see the following implementation to get better understanding −Example Live Democlass Solution: def solve(self, nums): ... Read More
Suppose we have a string s with only "a" and "b". "a"s can stay "a" or turn into "b", but "b"s can not be changed. We have to find the number of unique strings that we can make.So, if the input is like s = "baab", then the output will be 4, as We can make these strings − ["baab", "babb", "bbab", "bbbb"]To solve this, we will follow these steps −counts := frequency of 'a' in sreturn 2^countsLet us see the following implementation to get better understanding −Example Live Democlass Solution: def solve(self, s): counts = s.count('a') ... Read More
Suppose we have a number n, we have to check whether its prime factors only include 2, 3 or 5 or not.So, if the input is like n = 18, then the output will be True, as 18's prime factors are 2 and 3.To solve this, we will follow these steps −if n < 0, thenreturn Falsefactor := a list with elements [2, 3, 5]for each i in factor, dowhile n mod i is same as 0, don := n / ireturn true when n is same as 1, otherwise falseLet us see the following implementation to get better understanding ... Read More
Suppose we have a number n, we have to find a string of stairs with n steps. Here each line in the string is separated by a newline separator.So, if the input is like n = 5, then the output will be * ** *** **** *****To solve this, we will follow these steps −s := blank stringfor i in range 0 to n-1, dos := s concatenate (n-i-1) number of blank space concatenate ... Read More
Suppose we have a matrix M, we have to check whether it is a Toeplitz matrix or not. As we know a matrix is said to be Toeplitz when every diagonal descending from left to right has the same value.So, if the input is like726372537then the output will be True.To solve this, we will follow these steps −for each row i except the last one, dofor each column except the last one, doif matrix[i, j] is not same as matrix[i+1, j+1], thenreturn Falsereturn TrueLet us see the following implementation to get better understanding −Example Live Democlass Solution: def solve(self, matrix): ... Read More