From dcb3a23a0ddb7b06f4d8b57b9c7cb6e31c0eb662 Mon Sep 17 00:00:00 2001 From: Mateusz Tutaj Date: Fri, 8 Dec 2023 18:02:50 +0100 Subject: [PATCH] Mateusz Tutaj late --- file157.py | 30 ------------------------------ file87.py | 35 ----------------------------------- function.py | 42 ++++++++++++++++++++++++++++++++++++++++++ main.py | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+), 65 deletions(-) delete mode 100644 file157.py delete mode 100644 file87.py create mode 100644 function.py create mode 100644 main.py diff --git a/file157.py b/file157.py deleted file mode 100644 index 649c872..0000000 --- a/file157.py +++ /dev/null @@ -1,30 +0,0 @@ -def enumerate(pi): - for hello in pi.a: - print(f'Next element: {hello}') -from file87 import * -try: - my_pi = pi_container() - pi_gen = foo(5) - my_pi.mth(pi_gen.__next__()) - my_pi.mth(pi_gen.__next__()) - my_pi.mth(pi_gen.__next__()) - my_pi.mth(pi_gen.__next__()) - my_pi_2 = pi_container() - my_pi.mth(pi_gen.__next__()) - my_pi.mth(pi_gen.__next__()) - my_pi.mth(pi_gen.__next__()) -except: - print('something went horribly wrong :(') -pIgEn3 = foo(194) -for the_variable_that_contains_next_approximations_of_pi_from_generator in range(23): - my_pi_2.mth(next(pIgEn3)) -my_pi_3 = pi_container() -pi_gen = foo(6) -my_pi_3.mth([i for i in list(pi_gen)]) -print('my first pi') -enumerate(my_pi) -print('my second pi') -enumerate(my_pi_2) -new_file = open('some-file.txt', 'w') -new_file.write(f'my best pi: {my_pi_3.a[-1]}') -new_file.close() diff --git a/file87.py b/file87.py deleted file mode 100644 index 40e209e..0000000 --- a/file87.py +++ /dev/null @@ -1,35 +0,0 @@ -b = 0 -c = 0 - - -class pi_container: - def __init__(self, a=list()): - self.a = a - - def mth(self, x): - if type(x) == list: - self.a += x - else: - self.a.append(x) - - -def foo(x): - global b - global c - b = 0 - c=1 - for hello in range(x): - if hello % 2 == 0: - b += 4 / c#this is a very important operation in calculateing pi according to documentation that is provided in a seperate file in this repository, please analyse this file before using! - else: - b -= 4 / c - c += 2 - yield b - yield 'finished' - -def enumerate(pi: pi_container): - for hello in pi.a: - print(hello) - - -print('All functions are defined') diff --git a/function.py b/function.py new file mode 100644 index 0000000..cc6771b --- /dev/null +++ b/function.py @@ -0,0 +1,42 @@ +class PiContainer: + def __init__(self, my_pi: list[float] or None = None): + if my_pi is None: + self.my_pies = [] + else: + self.my_pies = my_pi + + def add_pi(self, next_pies): + if isinstance(next_pies, list): + self.my_pies += next_pies + else: + self.my_pies.append(next_pies) + + @property + def best(self): + return self.my_pies[-1] + + +def calc_pi(approx_level): + """ + result = a+b /c + :param approx_level: + :return pi + """ + b = 0 + c = 1 + for i in range(approx_level): + if i % 2 == 0: + b += 4 / c + else: + b -= 4 / c + c += 2 + yield b + + +def enumerate_pi(pi: PiContainer): + for my_pi in pi.my_pies: + print(my_pi) + + +if __name__ == '__main__': + print('All functions are defined') diff --git a/main.py b/main.py new file mode 100644 index 0000000..38078ac --- /dev/null +++ b/main.py @@ -0,0 +1,36 @@ +from function import PiContainer, calc_pi + + +def enumerate_pi(pi: PiContainer): + for my_pi in pi.my_pies: + print(f'Next element: {my_pi}') + + +def main(): + my_pi = PiContainer() + pi_gen = calc_pi(approx_level=5) + try: + for next_pi in pi_gen: + my_pi.add_pi(next_pi) + except Exception as e: + print('Error:', e) + raise e + pi_gen = calc_pi(approx_level=194) + my_pi_2 = PiContainer() + needed_approx = 23 + for _ in range(needed_approx): + my_pi_2.add_pi(next(pi_gen)) + my_pi_3 = PiContainer() + pi_gen = calc_pi(approx_level=6) + my_pi_3.add_pi(list(pi_gen)) + print('my first pi') + enumerate_pi(my_pi) + print('my second pi') + enumerate_pi(my_pi_2) + + with open('pi-file.txt', 'w') as file: + file.write(f'my best pi: {my_pi_3.best}') + + +if __name__ == '__main__': + main()