Found 26504 Articles for Server Side Programming

Program to check whether list is alternating increase and decrease or not in Python

Arnab Chakraborty
Updated on 14-Oct-2021 10:14:55

806 Views

Suppose we have a list of numbers called nums. We have to check whether the list alternates starting from strictly increasing then strictly decreasing and then strictly increasing and so on. And also if the list is only strictly increasing, it will be valid.So, if the input is like nums = [2, 4, 8, 7, 5, 1, 5, 7, 2, 1], then the output will be True, because [2, 4, 8] are increasing, then [7, 5, 1] is decreasing, then again [5, 7] is increasing and [2, 1] is decreasing.To solve this, we will follow these steps −if nums[1] = ... Read More

Program to find squared elements list in sorted order in Python

Arnab Chakraborty
Updated on 14-Oct-2021 10:11:57

689 Views

Suppose we have a list of numbers called nums, where elements are sorted in ascending order, we have to square the elements and return the result in sorted order.So, if the input is like nums = [-8, -3, 0, 5, 6], then the output will be [0, 9, 25, 36, 64]To solve this, we will follow these steps −n := size of numsl := 0r := n - 1index := n - 1res := a list of size same as nums and fill it with 0while index >= 0, doif |nums[l]| > |nums[r]|, thenres[index] := nums[l] * nums[l]l := l ... Read More

Program to sort numbers based on 1 count in their binary representation in Python

Arnab Chakraborty
Updated on 14-Oct-2021 10:08:39

5K+ Views

Suppose we have a lists of numbers in nums. We have to sort the list in ascending order by the number of 1s present in the binary representation for each number. If two numbers have same number of 1s, then arrange them based on their values.So, if the input is like nums = [4, 1, 12, 7, 6], then the output will be [1, 4, 6, 12, 7], because −Binary form of 4 is 0100Binary form of 1 is 0001Binary form of 6 is 0110Binary form of 12 is 1100Binary form of 7 is 0111So the arrangement is [1, 4, ... Read More

Program to find smallest pair sum where distance is not consecutive in Python

Arnab Chakraborty
Updated on 14-Oct-2021 10:06:34

279 Views

Suppose we have a list of numbers called. Now let us consider any pair of indices (i, j) where i < j and j - i > 1. Then find the smallest pair sum.So, if the input is like nums = [3, 4, 2, 2, 4], then the output will be 5, we can select values 3 and 2 so the total sum is 5. We cannot select 2 and 2 because they are adjacent, and violating the j - i > 1 constraint.To solve this, we will follow these steps −n := size of numsmin_seen := nums[0]ans := inffor ... Read More

Program to form smallest number where no two adjacent digits are same in Python

Arnab Chakraborty
Updated on 14-Oct-2021 10:02:35

371 Views

Suppose we have a string s with four possible characters "1", "2", "3" and "?". We can place any one of "1", "2" and "3", in the place of "?". We have to find the smallest possible number that we can make such that no two adjacent digits are same.So, if the input is like s = "2??3?", then the output will be 21231To solve this, we will follow these steps −i := 0s := a list of elements from sif size of s < 2, thenif s[i] is same as "?", thenreturn "1"while i < size of s, doif ... Read More

Program to find length of shortest sublist with maximum frequent element with same frequency in Python

Arnab Chakraborty
Updated on 14-Oct-2021 09:56:00

224 Views

Suppose we have a list of numbers called nums. If the frequency of a most frequent number in nums is k. We have to find the length of a shortest sublist such that the frequency of its most frequent item is also k.So, if the input is like nums = [10, 20, 30, 40, 30, 10], then the output will be 3, because here the most frequent numbers are 10 and 30 , here k = 2. If we select the sublist [30, 40, 30] this is the shortest sublist where 30 is present and its frequency is also 2.To ... Read More

Program to check right rotation forms increasing or decreasing array with first n natural numbers or not in Python

Arnab Chakraborty
Updated on 14-Oct-2021 09:53:40

134 Views

Suppose we have a list of numbers called nums, where n elements are present. We have to chesk whether we can make a list with first n natural numbers either in increasing or decreasing fashion, like [1, 2, ..., n] or [n, n - 1, ..., 1] by shifting nums to the right any number of times or not.So, if the input is like nums = [5, 6, 1, 2, 3, 4], then the output will be True, because we can shift them four times to make the array [1, 2, 3, 4, 5, 6]To solve this, we will follow ... Read More

Program to define set data structure without using library set class in Python

Arnab Chakraborty
Updated on 14-Oct-2021 09:51:20

303 Views

Suppose we want to implement a set data structure with the following methods −Constructor to construct a new instance of a setadd(val) to insert integer val into the setexists(val) to check whether val is in the set or notremove(val) to remove the val from the setSo, if we construct a set s, then call s.add(10), s.add(20), s.add(10), s.exists(10), s.remove(10), s.exists(10), s.exists(20), then the output will befor s.add(10) it will insert 10for s.add(20) it will insert 2010 is already in s, so nothing will happens.exists(10) will return true as 10 is thereDelete 10 by s.remove(10)s.exists(10) will return false because 10 is ... Read More

Program to check whether all can get a seat or not in Python

Arnab Chakraborty
Updated on 14-Oct-2021 09:48:20

601 Views

Suppose we have a number n, there are n number of people searching for a seat, we also have a list of bits where 1 represents an already occupied seat and 0 represents empty seat. No two people can sit next to each other, so we have to check whether all n people can find a seat or not.So, if the input is like n = 2 seats = [1, 0, 0, 0, 1, 0, 0], then the output will be True, because they can seat at index 2 and 6.To solve this, we will follow these steps −insert 0 ... Read More

Program to implement run length string decoding iterator class in Python

Arnab Chakraborty
Updated on 14-Oct-2021 09:44:59

241 Views

Suppose we want to define an iterator class that constructs with a run-length encoded lowercase string say s, there are two functions for this iterator they are −next() this finds the next element in the iteratorhasnext() this checks whether the next element is present or notSo, if the input is like s = "2b1a", then construct an object with s, then call next(), hasnext(), next(), next(), hasnext(), then the output will be "b", True, "b", "a", False.To solve this, we will follow these steps −Define a constructor. This will take soutput := a new listnum := blank stringfor each i ... Read More

Advertisements