Found 10783 Articles for Python

Book Pagination in Python

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

1K+ Views

Suppose we have a list of strings called book, if we page an index (0-indexed) into the book, and page_size, we have to find the list of words on that page. If the page is out of index then simply return an empty list.So, if the input is like book = ["hello", "world", "programming", "language", "python", "c++", "java"] page = 1 page_size = 3, then the output will be ['language', 'python', 'c++']To solve this, we will follow these steps −l:= page*page_sizereturn elements of book from index l to l+page_size - 1Let us see the following implementation to get better understanding ... Read More

Bob's Game in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:11:41

424 Views

Suppose we have a friend named Bob, and he is playing a game with himself. He gives himself a list of numbers called nums. Now in each turn, Bob selects two elements of the list and replaces them with one positive integer with the same sum as the numbers he selected. Bob declares the victory when all of the number in the array are even. We have to find the minimum number of turns are required to make by Bob, so he can declare victory, if there is no such solution, then return -1.So, if the input is like [2, ... Read More

Big Numbers in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:09:40

291 Views

Suppose we have a matrix, we have to find the total number of integers whose value is the largest in its row and column.So, if the input is like132465157then the output will be 2 as 6 and 7 are valid.To solve this, we will follow these steps −mat := matrixr_maxes := make a list of max elements of each row of matc_maxes := make a list of max elements of each column of mata := a new listfor r in range 0 to number of rows - 1, dofor c in range 0 to number of columns - 1, dov ... Read More

Base 3 to integer in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:06:55

813 Views

Suppose we have a string s that is representing a number in base 3 (valid numbers 0, 1, or 2), we have to find its equivalent decimal integer.So, if the input is like "10122", then the output will be 98.To solve this, we will follow these steps −ans := 0for each digit c in s, doans := 3 * ans + creturn ansLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s):       ans = 0       for c in map(int, s):          ans = 3 * ans + c       return ans ob = Solution() print(ob.solve("10122"))Input"10122"Output98

Austin Powers in Python

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

72 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

A unique string in Python

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

725 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

A strictly increasing linked list in Python

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

220 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

507 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

125 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

Advertisements