Suppose we have a string s of size n. We have to find all rotated strings by rotating them 1 place, 2 places ... n places.So, if the input is like s = "hello", then the output will be ['elloh', 'llohe', 'lohel', 'ohell', 'hello']To solve this, we will follow these steps −res := a new listn := size of sfor i in range 0 to n, dos := (substring of s from index 1 to n-1) concatenate s[0]insert s at the end of resreturn resExampleLet us see the following implementation to get better understanding −def solve(s): res = [] ... Read More
Suppose we have a number n. We have to find the sum of first n Fibonacci terms (Fibonacci sequence up to n terms). If the answer is too large then return result modulo 10^8 + 7.So, if the input is like n = 8, then the output will be 33 as first few Fibonacci terms are 0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 = 33To solve this, we will follow these steps −m := 10^8+7memo := a new mapDefine a function solve() . This will take n, mif n is in memo, ... Read More
Suppose we have a list of elements called nums, and a value k. We have to find those elements which have occurred at least k number of times.So, if the input is like nums = [2, 5, 6, 2, 6, 1, 3, 6, 3, 8, 2, 5, 9, 3, 5, 1] k = 3, then the output will be [2, 5, 6, 3]To solve this, we will follow these steps −c := a list containing frequencies of each elements present in numsres := a new listfor each key n in c, doif c[n] >= k, theninsert n at the end ... Read More
Suppose we have a list of rational numbers. We have to find their product using reduce function. The reduce() function applies a function with two arguments cumulatively on a list of objects from left to right.So, if the input is like fractions = [(5, 3), (2, 8), (6, 9), (5, 12), (7, 2)], then the output will be (175, 432) because 5/3 * 2/8 * 6/9 * 5/12 * 7/2 = (5*2*6*5*7)/(3*8*9*12*2) = 2100/5184 = 175/432.To solve this, we will follow these steps −fracs := a new listfor each f in frac, doinsert a new fraction object from (numerator, denominator) ... Read More
Suppose we have an email address as string. We have to check whether this is valid or not based on the following conditions −The format must be username@company.domain formatUsername can only contain upper and lowercase letters, numbers, dashes and underscoresCompany name can only contain upper and lowercase letters and numbersDomain can only contain upper and lowercase lettersMaximum length of the extension is 3.We can use regular expression to validate the mail addresses. Regular expressions can be used by importing re library. To match a pattern we shall use match() function under re library.So, if the input is like s = ... Read More
Suppose we have a number x and another number n. We have to find number of ways we can get x as sum of nth power of some unique numbers.So, if the input is like x = 100 n = 2, then the output will be 3 because possible solutions are 6^2 + 8^2, 10^2 and 1^2 + 3^2 + 4^2 + 5^2 + 7^2.To solve this, we will follow these steps −ans := 0Define a method called solve() this will take four parameters x, n, cn and cs, the initialvalues for cs = 0, cn = 1p := cn^nwhile ... Read More
Suppose we have one alphanumeric string s. We have to sort it based on following conditionAll sorted lowercase letters will be placed before uppercase letters.All sorted uppercase letters will be placed before digits.All sorted odd digits will be placed before sorted even digits.So, if the input is like s = "HeLlo1234", then the output will be eloHL1324To solve this, we will follow these steps −Define a function f() . This will take ccode := 0if c is in upper case, thencode := 10^3otherwise when c is a digit, thencode := 10^6if ASCII of c is even, thencode := 10^9return code ... Read More
Suppose we have a 2d list containing information about athletes. This information is rank, age, height. Each row contains information for different athletes. We also have another number k. We have to sort the data based on kth attribute.So, if the input is likeRankageheight125190235180333185426175535180And k = 1.then the output will beRankageheight125190426175333185235180535180[[1, 25, 190], [4, 26, 175], [3, 33, 185], [2, 35, 180], [5, 35, 180]]To solve this, we will follow these steps −Call the sort() function for the 2d array called infodefine one function that sort based on kth argument and pass it to the key parameter of sort() function.ExampleLet ... Read More
Suppose we want to do complex number tasks by defining a complex number class with following operations −add() to add two complex numberssub() to subtract two complex numbersmul() to multiply two complex numbersdiv() to divide two complex numbersmod() to get modulus of complex numbersThe complex numbers will be shown in the form (a + bi). We have two complex numbers, will perform these operations on them. Inside the class we overload the add(), sub(), mul() and div() methods so that we can use the operators to perform the operations. We also overload __str__() method to print the complex number in ... Read More
Suppose we have two strings s and t (both contains lowercase English letters). We have to find a list of pairs of size 3, where each pair is in this form (l, k) here k is a string and l is its length. Now among these three pairs, first one contains substring of s and t which is longest common prefix p of these two strings, then the remaining part of s is s' and remaining part of t is t'. So final list will be like [(length of p, p), (length of s', s'), (length of t', t')].So, if ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP