Server Side Programming Articles - Page 1557 of 2650

Integer to Base 3 Number in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:19:05

3K+ Views

Suppose we have a number n, we have to find the base 3 equivalent of this number as string.So, if the input is like 17, then the output will be 122.To solve this, we will follow these steps −if n

In-place Move Zeros to End of List in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:16:15

3K+ Views

Suppose we have a list of numbers nums, we have to put all the zeros to the end of the list by updating the list in-place. And the relative ordering of other elements should not be changed. We have to try to solve this in O(1) additional space.So, if the input is like [2, 0, 1, 4, 0, 5, 6, 4, 0, 1, 7], then the output will be [2, 1, 4, 5, 6, 4, 1, 7, 0, 0, 0]To solve this, we will follow these steps −if size of L is same as 0, thenreturn a blank listk := ... Read More

Index into an Infinite String in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:14:26

468 Views

Suppose we have a string s and two integers i and j (i < j). Now suppose p is an infinite string of s repeating forever. We have to find the substring of p from indexes [i, j).So, if the input is like s = "programmer", i = 4, j = 8, then the output will be "ramm".To solve this, we will follow these steps −p:= blank stringfor t in range i to j, dop := p concatenate a character from s at index (t mod size of s)return pLet us see the following implementation to get better understanding −Example Live ... Read More

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

511 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

599 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

930 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

281 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

95 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

Advertisements