Found 10476 Articles for Python

Generate a list of Primes less than n in Python

Arnab Chakraborty
Updated on 23-Sep-2020 06:53:11

5K+ Views

Suppose we have a number n, we have to generate a list of all prime numbers smaller than or equal to n in ascending order. We have to keep in mind that 1 is not a prime number.So, if the input is like 12, then the output will be [2, 3, 5, 7, 11].To solve this, we will follow these steps −sieve := a list of size n+1 and fill with Trueprimes := a new list, initially blankfor i in range 2 to n, doif sieve[i] is True, theninsert i at the end of primesfor j in range i to ... Read More

Number of Programmer Worked on given Time in Python

Arnab Chakraborty
Updated on 23-Sep-2020 06:51:56

85 Views

Suppose we have list of intervals and another input time. In each interval the structure is [start, end], this is representing the times when a programmer worked. We have to find the number of programmers that were working at time.So, if the input is like interval = [[2, 6], [4, 10], [5, 9], [11, 14]], time = 5, then the output will be 3 as at time 5, there are three programmers, working [2, 6], [4, 10], [5, 9]To solve this, we will follow these steps −count := 0for each interval in intervals, doif start of interval = time, thencount ... Read More

Justify Frame Width in Python

Arnab Chakraborty
Updated on 23-Sep-2020 06:48:00

89 Views

Suppose we have a list of words, we have to frame it in a rectangular region, line by line. See the example for better understanding.So, if the input is like ['hello', 'world', 'python', 'programming', 'nice'], then the output will be*************** * hello       * * world       * * python      * * programming * * nice        * ***************To solve this, we will follow these steps −l:= length of maximum size word in the arrayst:= put star (l+4) timesfor each i in words, dost := st concatenate '*' concatenate i then add ... Read More

Flip and Invert Matrix in Python

Arnab Chakraborty
Updated on 23-Sep-2020 06:45:51

768 Views

Suppose we have a binary matrix mat. We have to select each row in matrix, then reverse the row. After that, flip each bit (0 to 1 and 1 to 0).So, if the input is like110010001then the output will be100101011To solve this, we will follow these steps −track:= 0for each row in mat, doreverse the rowtracker := 0for each val in row, doif val is 1, thenmat[track, tracker] := 0otherwise, mat[track, tracker] := 1tracker := tracker + 1track := track + 1return matLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, mat):   ... Read More

Replace Multiple of 3 and 5 With Fizz, Buzz in Python

Arnab Chakraborty
Updated on 23-Sep-2020 06:36:31

5K+ Views

Suppose we have a number n. We have to find a string that is representing all numbers from 1 to n, but we have to follow some rules.When the number is divisible by 3, put Fizz instead of the numberWhen the number is divisible by 5, put Buzz instead of the numberWhen the number is divisible by 3 and 5 both, put FizzBuzz instead of the numberTo solve this, we will follow these steps −For all number from 1 to n, if number is divisible by 3 and 5 both, put “FizzBuzz”otherwise when number is divisible by 3, put “Fizz”otherwise ... Read More

Python finding programming question in a String

Arnab Chakraborty
Updated on 22-Sep-2020 11:37:46

179 Views

Suppose we have a lowercase string s, we have to check whether it's possible to pick some subsequence of characters in s such that − 1. The difference of any two successive indexes of the characters is same 2. The characters form the string "programmingquestion"So, if the input is like "pzrzozgzrzazmzmziznzgzqzuzezsztzizozn", then the output will be TrueTo solve this, we will follow these steps −p := An array of indices where p is presentr := An array of indices where r is presentfor each j in p, dofor each k in r, doif k > j, thenif "programmingquestion" in substring ... Read More

Swap Consecutive Even Elements in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:33:11

596 Views

Suppose we have a list of numbers called nums, we have to exchange every consecutive even integer with each other.So, if the input is like nums = [4, 5, 6, 8, 10], then the output will be [6, 5, 4, 10, 8]To solve this, we will follow these steps −temp := nullfor i in range 0 to size of nums, doif nums[i] mod 2 is same as 0, thenif temp is not null, thenexchange nums[i], nums[temp]temp := nullotherwise, temp := ireturn numsLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, nums):     ... Read More

Remove Substrings in One Iteration in python

Arnab Chakraborty
Updated on 22-Sep-2020 11:31:32

164 Views

Suppose we have a string s, we have to remove all “y” and “xz” in the string in one iteration.So, if the input is like s = "xyxxzyyxxzx", then the output will be xxxxTo solve this, we will follow these steps −To solve this, we will follow these steps −temp := string after removing xzreturn temp after removing yLet us see the following implementation to get better understanding −Example Live Democlass Solution:    def solve(self, s):       return s.replace("xz","").replace("y","") ob = Solution() print(ob.solve("xyxxzyyxxzx"))Input"xyxxzyyxxzx"Outputxxxx

Pattern After Double, Reverse, and Swap in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:30:12

184 Views

Suppose we have a number n, we have ot find the nth value from the sequence. The sequence is like below −xxyxxyxxyyxxyxxxyyxyyxyyxyyxyyxyy...To generate the next value, we have to follow these rules, starting with xxy as the first term −When we are at the start of the pattern, double it(concatenate the string with itself).When the last operation was doubling, reverse it.When the last operation was reversing, exchange all xs with ys and vice versa.Repeat these steps.So, if the input is like n = 5, then the output will be "yyxyyxyyxyyx"To solve this, we will follow these steps −i := 0ret ... Read More

Domino Covering Board in Python

Arnab Chakraborty
Updated on 22-Sep-2020 11:26:36

235 Views

Suppose we have two numbers n and m representing a board of size n x m. We also have an unlimited number of 1 x 2 dominos. We have to find the maximum number of dominos that can be placed on the board such that they don't overlap and every domino lies completely within the board.So, if the input is like n = 5, m = 3, then the output will be 7To solve this, we will follow these steps −t := n * mreturn quotient of (t / 2)Let us see the following implementation to get better understanding −Example Live ... Read More

Advertisements