ahmet / Python-for-Data-Science
Herkese Açık Ahmet Kerem Piscine Final
in_out.py 972 B
def square(x: int | float) -> int | float:
"""Return the square of a number."""
return x * x
def pow(x: int | float) -> int | float:
"""Return the exponentiation of a number by himself."""
return x ** x
def outer(x: int | float, function) -> object:
"""Build a counter applying a function again on its own result."""
count = 0
def inner() -> float:
"""Apply the function once more and return the result."""
nonlocal count
count += 1
result = x
for _ in range(count):
result = function(result)
return result
return inner
def main() -> None:
"""Test the counters built by outer."""
my_counter = outer(3, square)
print(my_counter())
print(my_counter())
print(my_counter())
print("---")
another_counter = outer(1.5, pow)
print(another_counter())
print(another_counter())
print(another_counter())
if __name__ == "__main__":
main()