Found 10784 Articles for Python

Count Frequency of Highest Frequent Elements in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:09:58

2K+ Views

Suppose we have a list of numbers called nums, we have to find the most frequently present element and get the number of occurrences of that element.So, if the input is like [1, 5, 8, 5, 6, 3, 2, 45, 7, 5, 8, 7, 1, 4, 6, 8, 9, 10], then the output will be 3 as the number 5 occurs three times.To solve this, we will follow these steps −max:= 0length:= size of numsfor i in range 0 to length-2, docount:= 1for j in range i+1 to length-1, doif nums[i] is same as nums[j], thencount := count + 1if ... Read More

Insert 5 to Make Number Largest in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:08:28

415 Views

Suppose we have a number n, we have to find the maximum number we can make by inserting 5 anywhere in the number.So, if the input is like n = 826, then the output will be 8526.To solve this, we will follow these steps −temp := n as a stringans := -inffor i in range 0 to size of temp, docand := substring of temp from index 0 to i concatenate '5' concatenate substring of temp from index i to endif i is same as 0 and temp[0] is same as '-', thengo for the next iterationans := maximum of ... Read More

Guess Nearest Square Root in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:01:57

440 Views

Suppose we have a non-negative number n, we have to find a number r such that r * r = n and we have to round down to the nearest integer. We have to solve this problem without using the builtin square-root function.So, if the input is like 1025, then the output will be 32.To solve this, we will follow these steps −if n

Group Integers in Python

Arnab Chakraborty
Updated on 23-Sep-2020 06:59:26

721 Views

Suppose we have a list of numbers called nums, we have to check whether we can split the list into 1 or more groups such that: 1. Size of each group is greater than or equal to 2. 2. Sizes of all groups are same. 3. All the numbers present in each group are the same.So, if the input is like [3, 4, 6, 9, 4, 3, 6, 9], then the output will be True.To solve this, we will follow these steps −counts := a map where each key are distinct element and values are their frequenciestemp := 0for each ... Read More

Greatest common divisors in Python

Arnab Chakraborty
Updated on 23-Sep-2020 06:54:22

215 Views

Suppose we have a list of positive numbers called nums, we have to find the largest positive number that divides each of the number.So, if the input is like [14,28,70,56], then the output will be 14.To solve this, we will follow these steps −ans := first element of numsfor each x in nums, doans := gcd of ans and xreturn ansLet us see the following implementation to get better understanding −Exampleimport math class Solution:    def solve(self, nums):       ans = nums[0]       for x in nums:          ans = math.gcd(ans, x)       return ans ob = Solution() print(ob.solve([14,28,70,56]))Input[14,28,70,56]Output14

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

41 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

51 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

525 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

Advertisements