Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Produce K evenly spaced float values

Python – Produce K evenly spaced float values

Given a range and element K, generate K float values that are evenly spaced.

Input  :  i = 4, j = 6, K = 10
Output :  [4.2, 4.4, 4.6, 4.8, 5.0, 5.2, 5.4, 5.6, 5.8, 6.0]
Explanation :  The difference 0.2 is added after 4 to produce 10 elements till 6.

Input  : i = 10, j = 20, K = 2
Output :  [15.0, 20.0]
Explanation :  5 is difference and is added each time.

Method #1: Using loop

This is one of the ways in which this task can be performed. In this, we iterate till K while increasing the counter for difference between successive elements.

Python3




# Python3 code to demonstrate working of
# Produce K evenly spaced float values
# Using loop
 
# initializing range
i, j = 2, 10
 
# Initialize K
K = 15
 
# computing difference
diff = (j - i) / K
res = []
 
# using loop to add numbers to result
for idx in range(1, K + 1):
    res.append(i + (diff * idx))
 
# printing result
print("The constructed list : " + str(res))


Output

The constructed list : [2.533333333333333, 3.0666666666666664, 3.6, 4.133333333333333, 4.666666666666666, 5.2, 5.733333333333333, 6.266666666666667, 6.8, 7.333333333333333, 7.866666666666666, 8.4, 8.933333333333334, 9.466666666666667, 10.0]

Time Complexity: O(n) where n is the number of elements 
Auxiliary Space: O(n) where n is the number of elements in the new list.

Method #2: Using list comprehension

The working of this method is similar to above method.  The difference being compact way in which this solution is formulated using list comprehension.

Python3




# Python3 code to demonstrate working of
# Produce K evenly spaced float values
# Using loop
 
# initializing range
i, j = 2, 10
 
# Initialize K
K = 15
 
# computing difference
diff = (j - i) / K
 
# using list comprehension to formulate elements
res = [i + (diff * idx) for idx in range(1, K + 1)]
 
# printing result
print("The constructed list : " + str(res))


Output

The constructed list : [2.533333333333333, 3.0666666666666664, 3.6, 4.133333333333333, 4.666666666666666, 5.2, 5.733333333333333, 6.266666666666667, 6.8, 7.333333333333333, 7.866666666666666, 8.4, 8.933333333333334, 9.466666666666667, 10.0]

Time Complexity: O(n) where n is the number of elements in the list 
Auxiliary Space: O(n) where n is the number of elements in the list

Method #3 : Using recursion+round()

In this method, we use list comprehension to produce k evenly spaced values between i and j. We calculate the step size by dividing the difference between j and i by k-1. We use this step size to generate k values using a for loop inside the list comprehension. Finally, we round the values to 1 decimal place using the round() function.

Python3




def evenly_spaced_floats(i, j, k):
    # Calculate the step size between each float value
    step = (j - i) / (k - 1)
     
    # Generate a list of K evenly spaced float values
    # by adding a multiple of the step size to the start value i
    # for each value in the range [0, K-1]
    # and rounding each value to 1 decimal place
    return [round(i + n*step, 1) for n in range(k)]
 
# Example usage
i = 4
j = 6
k = 10
 
result = evenly_spaced_floats(i, j, k)
print(result)


Output

[4.0, 4.2, 4.4, 4.7, 4.9, 5.1, 5.3, 5.6, 5.8, 6.0]

Time complexity: O(k)
Auxiliary Space: O(k)

Method #4: Using numpy:

  • Calculate the step size between each float value by subtracting the start value i from the end value j, and then dividing by the number of values to generate k minus 1. This will give us the increment between each value in the sequence.
  • Generate a 1D array of k evenly spaced float values. We can use a generator expression to compute each value by adding a multiple of the step size to the start value i for each value in the range [0, k-1], and rounding each value to 1 decimal place. Then, we can use the np.fromiter function to create a numpy array from the generator expression.
  • Return the resulting numpy array of evenly spaced float values.

Python3




import numpy as np
 
def evenly_spaced_floats(i, j, k):
    # Calculate the step size between each float value
    step = (j - i) / (k - 1)
 
    # Generate a 1D array of K evenly spaced float values
    # by adding a multiple of the step size to the start value i
    # for each value in the range [0, K-1]
    # and rounding each value to 1 decimal place
    return np.fromiter((i + n*step for n in range(k)), dtype=float)
 
# Example usage
i = 4
j = 6
k = 10
 
result = evenly_spaced_floats(i, j, k)
print(result)
#This code is contributed by Jyothi Pinjala.


Output:

[4.         4.22222222  4.44444444  4.66666667  4.88888889  5.11111111
5.33333333  5.55555556  5.77777786.        ]

Time complexity: O(k) since it involves generating a list of K evenly spaced float values, which requires iterating over a range of K values.
Auxiliary space: O(k) since it involves creating a list of K float values to store the generated values.

Method #5: Using map function with lambda expression

  • Define a function named “evenly_spaced_floats”
  • Calculate the step size between each float value
  • Generate a list of K integers from 0 to K-1 using the range function
  • Use the map function with lambda expression to generate a list of K evenly spaced float values
  • Round each value to 1 decimal place using the round function
  • Return the list of evenly spaced float values

Python3




def evenly_spaced_floats(i, j, k):
    # Calculate the step size between each float value
    step = (j - i) / (k - 1)
     
    # Generate a list of K integers from 0 to K-1
    indices = range(k)
     
    # Use the map function with lambda expression to generate a list of K evenly spaced float values
    values = map(lambda n: round(i + n*step, 1), indices)
     
    # Convert the map object to a list and return it
    return list(values)
 
# Example usage
i = 4
j = 6
k = 10
 
result = evenly_spaced_floats(i, j, k)
print(result)


Output

[4.0, 4.2, 4.4, 4.7, 4.9, 5.1, 5.3, 5.6, 5.8, 6.0]

Time complexity: O(k), as we need to iterate over the K values to generate the list of evenly spaced float values.
Auxiliary space: O(k), as we need to store the list of K integers and the list of K evenly spaced float values.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments