Found 10476 Articles for Python

Caesar Cipher in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:20:03

1K+ Views

Suppose we have a lowercase alphabet string s, and an offset number say k. We have to replace every letter in s with a letter k positions further along the alphabet. We have to keep in mind that when the letter overflows past a or z, it gets wrapped around the other side.So, if the input is like "hello", k = 3, then the output will be "khoor"To solve this, we will follow these steps −Define a function shift(). This will take ci := ASCII of (c) - ASCII of ('a')i := i + ki := i mod 26return character ... Read More

Buying Cars in Python

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

405 Views

Suppose we have a list of prices of cars for sale, and we also have a budget k, we have to find the maximum number of cars we can buy.So, if the input is like [80, 20, 10, 30, 80], k = 85, then the output will be 3 as we can buy three cars with prices 20, 10, 40To solve this, we will follow these steps −count := 0sort the list pricesfor i in range 0 to size of prices, doif prices[i]

Boss Fight in Python

Arnab Chakraborty
Updated on 02-Sep-2020 13:15:57

646 Views

Suppose we have a binary list called fighters and another list of binary lists called bosses. In fighters list the 1 is representing a fighter. Similarly, in bosses list 1 representing a boss. That fighters can beat a boss’s row if it contains more fighters than bosses. We have to return a new bosses matrix with defeated boss rows removed.So, if the input is like fighters = [0,1,1]011000001011111then the output will be011111To solve this, we will follow these steps −fighter_cnt := sum of all elements of fightersresult := a new listfor each row in bosses, doif fighter_cnt

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

640 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

397 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

1K+ 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

110 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

961 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

Advertisements