Found 10476 Articles for Python

Program to find length of longest sublist containing repeated numbers by k operations in Python

Arnab Chakraborty
Updated on 11-Oct-2021 08:12:58

262 Views

Suppose we have a list called nums and a value k, now let us consider an operation by which we can update the value of any number in the list. We have to find the length of the longest sublist which contains repeated numbers after performing at most k operations.So, if the input is like nums = [8, 6, 6, 4, 3, 6, 6] k = 2, then the output will be 6, because we can change 4 and 3 to 6, to make this array [8, 6, 6, 6, 6, 6, 6], and the length of sublist with all ... Read More

Program to find length of longest contiguous sublist with same first letter words in Python

Arnab Chakraborty
Updated on 11-Oct-2021 08:10:34

215 Views

Suppose we have a list of lowercase alphabet strings called words. We have to find the length of the longest contiguous sublist where the first letter of each words have the same first letter.So, if the input is like words = ["she", "sells", "seashells", "on", "the", "sea", "shore"], then the output will be 3, the longest contiguous sublist is ["she", "sells", "seashells"]. The first letter for each words is 's'.To solve this, we will follow these steps −cnt := 1maxcnt := 0prev_char := blank stringfor each word in words, doif prev_char is empty, thenprev_char := first letter of wordotherwise when ... Read More

Matplotlib – How to plot the FFT of signal with correct frequencies on the X-axis?

Rishikesh Kumar Rishi
Updated on 11-Oct-2021 08:12:10

3K+ Views

To plot the FFT (Fast Fourier Transform) of a signal with correct frequencies on the X-axis in matplotlib, we can take the following steps −StepsSet the figure size and adjust the padding between and around the subplots.Initialize two variables, N and m, to calculate nu.Create the signal (a sine wave) using numpy. Compute the one-dimensional discrete Fourier Transform.Return the Discrete Fourier Transform sample frequencies.Plot the freq and fourier transform data points.To display the figure, use Show() method.Exampleimport numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True N = 256 t = np.arange(N) m ... Read More

Program to find number of possible position in n-person line with few person at front and back in Python

Arnab Chakraborty
Updated on 11-Oct-2021 08:08:03

157 Views

Suppose we have three numbers n, a and b. Consider we are in a line of n people. And we are unaware about our position in the line. But we know there are at least a number of people in front of us and at most b number of people behind us. We have to find the number of possible positions possible for us.So, if the input is like n = 10 a = 3 b = 4, then the output will be 5, because there are 10 people in the line and at least 3 are in front and ... Read More

Program to find lexicographically largest mountain list in Python

Arnab Chakraborty
Updated on 11-Oct-2021 08:06:33

269 Views

Suppose we have three positive numbers say n, lower, and upper. We have to find a list whose length is n and that is strictly increasing and then strictly decreasing and all the numbers are in range [lower and upper] (both inclusive). And each increasing and decreasing parts should be non-empty. We have to find the lexicographically largest such list possible, if this is not possible, then return empty list.So, if the input is like n = 5 lower = 3 upper = 7, then the output will be [6, 7, 6, 5, 4], if we look closely, the [7, ... Read More

Program to find maximum sum by performing at most k negate operations in Python

Arnab Chakraborty
Updated on 11-Oct-2021 08:04:36

254 Views

Suppose we have a list of elements called nums we also have another value k. Now let us consider an operation where we select an element from nums and negate it. We can perform exactly k number of operations. We have to find the maximum resulting sum that can be generated.So, if the input is like nums = [2, 1, -6, -2] k = 3, then the output will be 9, if we negate -6 and -2 and 1 shall get [2, -1, 6, 2] and its sum is 9.To solve this, we will follow these steps −n := size ... Read More

Program to find value for which given array expression is maximized in Python

Arnab Chakraborty
Updated on 11-Oct-2021 08:01:04

246 Views

Suppose we have two arrays called nums and values, both contains integers and the values of nums are strictly increasing and their lengths are also same. We have to find the value of v for a pair of indices i, j, such that: i ≤ j that maximizes v = values[i] + values[j] + nums[j] - nums[i].So, if the input is like nums = [1, 2, 7] values = [-4, 6, 5], then the output will be 16, if we take pick i = 1 and j = 2 we get 6 + 5 + 7 - 2 = 16.To ... Read More

Program to find number of elements in matrix follows row column criteria in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:56:54

663 Views

Suppose we have a binary matrix; we have to find the number of elements in matrix that follows the following rules −matrix[r, c] = 1matrix[r, j] = 0 for every j when j is not equal to c and matrix[i, c] = 0 for every i when i is not equal to r.So, if the input is like001100010then the output will be 3, because we have cells (0, 2), (1, 0) and (2, 1) those meet the criteria.To solve this, we will follow these steps −if matrix is empty, thenreturn 0row := a list of sum of all row entries ... Read More

Program to find k where k elements have value at least k in Python

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

324 Views

Suppose we have a list of numbers called nums, that contains only non-negative numbers. If there are exactly k number of elements in nums that are greater than or equal to k, find the value k. If we cannot find such, then return -1.So, if the input is like nums = [6, 4, 0, 8, 2, 9], then the output will be 4, because there are exactly 4 elements that are greater than or equal to 4: [6, 4, 8, 9].To solve this, we will follow these steps −sort the list nums in reverse orderfor i in range 1 to ... Read More

Program to find array of length k from given array whose unfairness is minimum in python

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

586 Views

Suppose we have an array A and another value k. We have to form an array arr whose size is k bu taking elements from A and minimize the unfairness. Here the unfairness is calculated by this formula −(𝑚𝑎𝑥𝑖𝑚𝑢𝑚 𝑜𝑓 𝑎𝑟𝑟) − (𝑚𝑖𝑛𝑖𝑚𝑢𝑚 𝑜𝑓 𝑎𝑟𝑟)So, if the input is like A = [25, 120, 350, 150, 2500, 25, 35] and k = 3, then the output will be 10, because we can take elements [25, 25, 35] so max(arr) = 35 and min(arr) = 25. So their difference is 10.To solve this, we will follow these steps −i:= 0sort the ... Read More

Advertisements