Server Side Programming Articles - Page 875 of 2650

Program to check given number is a Fibonacci term in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:20:43

2K+ Views

Suppose we have a number say n. We have to check whether n is present in Fibonacci sequence or not. As we know in Fibonacci sequence f(i) = f(i-1) + f(i-2) for each i from 2 to n, and f(0) = 0, f(1) = 1.So, if the input is like n = 13, then the output will be True, as some terms in Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, so 34 is present.To solve this, we will follow these steps −phi := 0.5 + 0.5 * square root of(5.0)a := phi * nreturn ... Read More

Program to find local peak element indices from a list of numbers in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:17:17

445 Views

Suppose we have a list of numbers called nums whose length is at least 2. We have to find the index of every peak in the list. The list is sorted in ascending order. An index i is a peak when −nums[i] > nums[i + 1] when i = 0nums[i] > nums[i - 1] when i = n - 1nums[i - 1] < nums[i] > nums[i + 1] elseSo, if the input is like nums = [5, 6, 7, 6, 9], then the output will be [2, 4], as the element at index 2 is 7 which is larger than ... Read More

Program to check whether elements frequencies are even or not in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:14:01

112 Views

Suppose we have a list of elements called nums, we have to check whether all numbers appear even times or not. We have to solve it using constant space.So, if the input is like nums = [8, 9, 9, 8, 5, 5], then the output will be True, because all number have occurred twice.To solve this, we will follow these steps −if size of nums is odd, thenreturn Falsesort the list numsfor i in range 1 to size of nums, doif nums[i] is same as nums[i - 1], thennums[i] := 0, nums[i - 1] := 0return true when sum of ... Read More

Program to check same value and frequency element is there or not in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:12:26

168 Views

Suppose we have a list of numbers called nums, we have to check whether there is an element whose frequency is same as its value.So, if the input is like nums = [2, 5, 7, 5, 3, 5, 3, 5, 9, 9, 5], then the output will be True, because 5 appears 5 times.To solve this, we will follow these steps −nums_c := a list containing frequencies of each elements present in numsfor each value i and frequency j in nums_c, doif i is same as j, thenreturn Truereturn FalseExampleLet us see the following implementation to get better understandingfrom collections ... Read More

Program to find multiple of n with only two digits in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:15:16

254 Views

Suppose we have a number n. We have to find the least positive value x such that x is made up of only two digits 9's and 0's, and x is multiple of n.So, if the input is like n = 26, then the output will be 90090.To solve this, we will follow these steps −m := 9x := 1while m is not divisible by n, dox := x + 1m := replace all 1s with 9s in the binary form of xreturn m as integerExampleLet us see the following implementation to get better understanding −def solve(n):    m = ... Read More

Program to count index pairs for which array elements are same in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:09:51

631 Views

Suppose we have a list of numbers called nums. We have to find the number of pairs i < j such that nums[i] and nums[j] are same.So, if the input is like nums = [5, 4, 5, 4, 4], then the output will be 4, as We have index pairs like (0, 2), (1, 3), (1, 4) and (3, 4).To solve this, we will follow these steps −c := a list containing frequencies of each elements present in numscount := 0for each n in list of all values of c, docount := count + floor of (n *(n - 1)) ... Read More

Program to sort given set of Cartesian points based on polar angles in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:05:51

1K+ Views

Suppose we have a set of Cartesian points in a list called points. We have to sort them based on their polar angles. The polar angles vary in range 0 and 2*PI. If some points have same polar angles, then arrange them based on the distance of that point from the origin.So, if the input is like points = [(1, 1), (1, -2), (-2, 2), (5, 4), (4, 5), (2, 3), (-3, 4)], then the output will be [(5, 4), (1, 1), (4, 5), (2, 3), (-3, 4), (-2, 2), (1, -2)]To solve this, we will follow these steps −Define ... Read More

Program to find duplicate item from a list of elements in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:00:09

209 Views

Suppose we have a list of elements called nums of size n + 1, they are selected from range 1, 2, ..., n. As we know, by pigeonhole principle, there must be a duplicate. We have to find the duplicate. Here our target is to find the task in O(n) time and constant space.So, if the input is like nums = [2, 1, 4, 3, 5, 4], then the output will be 4To solve this, we will follow these steps −q := sum of all elements present in numsn := size of numsv := floor of ((n - 1)*(n)/2)return q ... Read More

Program to count elements whose next element also in the array in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:57:47

226 Views

Suppose we have a list of numbers say nums, we have to find the number of elements x in the array, such that x + 1 also exists in the array.So, if the input is like nums = [4, 2, 3, 3, 7, 9], then the output will be 3, because 2+1 = 3 is present, 3+1 = 4 is present and another 3 is present so total 3.To solve this, we will follow these steps −answer := 0c := a list containing frequencies of each elements present in numsdlist := a list from list of all keys of cfor ... Read More

Program to check all 1s are present one after another or not in Python

Arnab Chakraborty
Updated on 11-Oct-2021 06:55:36

148 Views

Suppose we have a list of numbers called nums that contains at least one element whose value is 1. We have to check whether all the 1s appear consecutively or not.So, if the input is like nums = [8, 2, 1, 1, 1, 3, 5], then the output will be True.To solve this, we will follow these steps −visited := 0for each x in nums, doif x is same as 1, thenif visited is same as 2, thenreturn Falsevisited := 1otherwise when visited is non-zero, thenvisited := 2return TrueExampleLet us see the following implementation to get better understandingdef solve(nums): ... Read More

Advertisements