Austin Powers in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:05:18

113 Views

Suppose we have a number greater than 0, we have to check whether the number is power of two or not.So, if the input is like 1024, then the output will be True.To solve this, we will follow these steps −while n > 1, don := n / 2return true when n is same as 1, otherwise 0Let us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, n):       while n > 1:          n /= 2       return n == 1 ob = Solution() print(ob.solve(1024))Input1024OutputTrue

Unique String in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:02:51

964 Views

Suppose we have a string s, we have to check whether it has all unique characters or not.So, if the input is like "world", then the output will be TrueTo solve this, we will follow these steps −set_var := a new set from all characters of sreturn true when size of set_var is same as size of s, otherwise falseLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s):       set_var = set(s)       return len(set_var) == len(s) ob = Solution() print(ob.solve('hello')) print(ob.solve('world'))Inputhello worldOutputFalse TrueRead More

Atbash Cipher in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:00:21

2K+ Views

Suppose we have a lowercase alphabet string called text. We have to find a new string where every character in text is mapped to its reverse in the alphabet. As an example, a becomes z, b becomes y and so on.So, if the input is like "abcdefg", then the output will be "zyxwvut"To solve this, we will follow these steps −N := ASCII of ('z') + ASCII of ('a')return ans by joining each character from ASCII value (N - ASCII of s) for each character s in textLet us see the following implementation to get better understanding −Example Live Democlass Solution: ... Read More

Strictly Increasing Linked List in Python

Arnab Chakraborty
Updated on 02-Sep-2020 12:58:56

276 Views

Suppose we have head of a singly linked list, we have to check whether the values of the nodes are sorted in a strictly ascending order or not.So, if the input is like [2, 61, 105, 157], then the output will be True.To solve this, we will follow these steps −Define a function solve() . This will take headif head.next is null, thenreturn Trueif head.val >= head.next.val, thenreturn Falsereturn solve(head.next)Let us see the following implementation to get better understanding −Example Live Democlass ListNode:    def __init__(self, data, next = None):       self.val = data       self.next = ... Read More

A Number and Its Triple in Python

Arnab Chakraborty
Updated on 02-Sep-2020 12:56:08

701 Views

Suppose we have a list of numbers called nums, we have to check whether there are two numbers such that one is a triple of another or not.So, if the input is like nums = [2, 3, 10, 7, 9], then the output will be True, as 9 is the triple of 3To solve this, we will follow these steps −i := 0sort the list nj := 1while j < size of n, doif 3*n[i] is same as n[j], thenreturn Trueif 3*n[i] > n[j], thenj := j + 1otherwise, i := i + 1return FalseLet us see the following implementation ... Read More

Ancient Astronaut Theory in Python

Arnab Chakraborty
Updated on 02-Sep-2020 12:52:14

204 Views

Suppose er have a string dictionary, the dictionary is representing a partial lexicographic ordering of ancient astronauts' dictionary. So, if we have a string s, we have to check whether it's a lexicographically sorted string according to this ancient astronaut dictionary or not.So, if the input is like dictionary = "bdc", s = "bbbb h ddd i cccc", then the output will be TrueTo solve this, we will follow these steps −l := size of astro_dictif l is same as 0, thenreturn Truei := 0for each character c in s, doif c in astro_dict, thenwhile i < l and astro_dict[i] ... Read More

Adding Time in Python

Arnab Chakraborty
Updated on 02-Sep-2020 12:45:26

4K+ Views

Suppose we have a string that is representing a 12-hour clock time with suffix am or pm, and an integer n is also given, we will add n minutes to the time and return the new time in the same format.So, if the input is like s = "8:20pm" and n = 150, then the output will be 10:50pmTo solve this, we will follow these steps −h, m := take the hour and minute part from sh := h mod 12if the time s is in 'pm', thenh := h + 12t := h * 60 + m + nh ... Read More

Acronym in Python

Arnab Chakraborty
Updated on 02-Sep-2020 12:42:46

1K+ Views

Suppose we have a string s that is representing a phrase, we have to find its acronym. The acronyms should be capitalized and should not include the word "and".So, if the input is like "Indian Space Research Organisation", then the output will be ISROTo solve this, we will follow these steps −tokens:= each word of s as an arraystring:= blank stringfor each word in tokens, doif word is not "and", thenstring := string concatenate first letter of wordreturn convert string into uppercase stringLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s):   ... Read More

3 and 7 in Python

Arnab Chakraborty
Updated on 02-Sep-2020 12:37:59

343 Views

Suppose we have a positive number n, we have to find that we can make n by summing up some non-negative multiple of 3 and some non-negative multiple of 7 or not.So, if the input is like 13, then the output will be True, as 13 can be written as 1*7+2*3 = 13To solve this, we will follow these steps −for i in range 0 to n+1, increase by 7, doif n-i is divisible by 3, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, n):       for i ... Read More

3-6-9 in Python

Arnab Chakraborty
Updated on 02-Sep-2020 12:35:51

886 Views

Suppose we have a number n, we have to construct a list with each number from 1 to n, except when it is multiple of 3 or has a 3, 6, or 9 in the number, it should be the string "no-fill".So, if the input is like 20, then the output will be ['1', '2', 'clap', '4', '5', 'clap', '7', '8', 'clap', '10', '11', 'clap', 'clap', '14', 'clap', 'clap', '17', 'clap', 'clap', '20']To solve this, we will follow these steps −string := "no-fill"ls:= make a list of numbers as string from 1 to nfor i in range 0 to size ... Read More

Advertisements