AP in Python using function
Question:
Write a program that generates a series using a function which takes first and last values of the series and then generates four term that are equidistant. in python.
Answers:
You can do that this way;
Also, you can specify how many terms you want in between first and last using the third argument.
Use simple mathematics.
def ap (arg1, arg2, d):
diff = arg2 - arg1
for i in range(d):
yield arg1 + i*(diff/(d+1)) # division by d+1
>>> ap (1, 6, 4))
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
Write a program that generates a series using a function which takes first and last values of the series and then generates four term that are equidistant. in python.
You can do that this way;
Also, you can specify how many terms you want in between first and last using the third argument.
Use simple mathematics.
def ap (arg1, arg2, d):
diff = arg2 - arg1
for i in range(d):
yield arg1 + i*(diff/(d+1)) # division by d+1
>>> ap (1, 6, 4))
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]