[Type C] Working with Functions – Chapter 3 Sumita Arora

|
Table of Contents

9. Write a program that generates a series using a function which takes the first and last values of the series and then generates four terms that are equidistant. For example, if two numbers passed are 1 and 7 then the function returns 1, 3, 5, 7.

Short Answer: The program generates a series of four equidistant terms between the given first and last values using a function.

Python
def generate_equidistant_series(first, last):
    interval = (last - first) / 4
    series = [first + i * interval for i in range(4)]
    series.append(last)  # Adding the last value to the series
    return series

# Test
print(generate_equidistant_series(1, 7))  # Example output: [1.0, 2.5, 4.0, 5.5, 7]

Explanation:

  • The function generate_equidistant_series calculates the interval between each term as (last - first) / 4.
  • It then generates a series of four equidistant terms between the first and last values using list comprehension.
  • The last value is appended to the series to ensure the series includes the specified last value.
  • The test case demonstrates how the function generates the equidistant series for the given input range.

Leave a Reply

Your email address will not be published. Required fields are marked *