You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
programming-basics-2022/02/03.py

37 lines
599 B

import doctest
from typing import Callable
# term :: int -> int
def cube(x: int) -> int:
"""
Evaluates cube of given number.
>>> cube(3)
27
>>> cube(5)
125
"""
return x * x * x
# term :: int -> int
def summation(
n: int,
term: Callable[[int], int],
) -> int:
"""
Sums first n numbers of series.
>>> summation(5, cube)
225
>>> summation(5, lambda k: 8 // ((4 * k - 3) * (4 * k - 1)))
2
"""
total = 0
for k in range(1, n + 1):
total += term(k)
return total
if __name__ == "__main__":
doctest.testmod()