From 0321a5e9fe028792c91d5eb3fe6ecda43dd560b9 Mon Sep 17 00:00:00 2001 From: nadavtzaysler <110530309+nadavtzaysler@users.noreply.github.com> Date: Wed, 5 Jun 2024 10:46:58 +0300 Subject: [PATCH 1/3] added re-factored code for clean code excercise --- main.py | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..3fe6be1 --- /dev/null +++ b/main.py @@ -0,0 +1,101 @@ +import math + +def calculate_sum(num: int) -> int: + if num <= 0: + return 0 + return sum(range(num)) + +def is_prime(num: int) -> bool: + for i in range(2, int(math.sqrt(num)) + 1): + if num % i == 0: + return False + return num > 2 + +def factorial(num: int) -> int | None: + if num < 0: + raise ValueError("Negative values are not supported.") + elif num == 0: + return 1 + else: + result = 1 + for i in range(1, num + 1): + result *= i + return result + +def recursive_factorial(num: int) -> int | None: + if num < 0: + raise ValueError("Negative values are not supported.") + elif num == 0 or num == 1: + return 1 + else: + return num * recursive_factorial(num-1) + +def print_numbers(num: int) -> None: + for i in range(num): + if is_prime(i): + print(f"{i} is prime") + else: + print(f"{i} is not prime") + +def calculate_power(base: int, exponent: int) -> int | None: + if exponent < 0: + raise ValueError("Exponent cannot be negative.") + elif exponent == 0: + return 1 + else: + return base ** exponent + +def calculate_square_root(base: int) -> int | float | None: + if base < 0: + raise ValueError("Cannot calculate square root of a negative number.") + else: + return math.sqrt(base) + +def calculate_hypotenuse(a: int, b: int) -> int | None: + if a < 0 or b < 0: + raise ValueError("Both sides must be positive.") + else: + return math.hypot(a, b) + +def calculate_fibonacci(num: int) -> int | None: + if num < 0: + raise ValueError("Negative values are not supported.") + elif num == 0: + return 0 + elif num == 1: + return 1 + else: + a, b = 0, 1 + for _ in range(2, num + 1): + a, b = b, a + b + return b + +def calculate_factorial_sum(num: int) -> int: + if num < 0: + raise ValueError("Negative values are not supported.") + else: + return sum(factorial(i) for i in range(num + 1)) + +def calculate_exponential_sum(exponent: int, num: int) -> int | None: + if num < 0: + raise ValueError("Number of terms cannot be negative.") + else: + return sum(calculate_power(exponent, i) / factorial(i) for i in range(num + 1)) + +def main(): + try: + num = int(input("Enter a number: ")) + print(f"Sum of numbers from 0 to {num}: {calculate_sum(num)}") + print(f"Factorial of {num}: {factorial(num)}") + print_numbers(num) + print(f"2^3 = {calculate_power(2, 3)}") + print(f"Square root of 16: {calculate_square_root(16)}") + print(f"Hypotenuse of 3 and 4: {calculate_hypotenuse(3, 4)}") + print(f"Fibonacci sequence up to 10: {calculate_fibonacci(10)}") + print(f"Factorial sum up to 5: {calculate_factorial_sum(5)}") + print(f"Exponential sum of 2 up to 5 terms: {calculate_exponential_sum(2, 5)}") + except ValueError as e: + print(f"Error: {e}") + +if __name__ == "__main__": + main() \ No newline at end of file From bfd7d3a4b530ca6344ccb3b647203790e67f1b6d Mon Sep 17 00:00:00 2001 From: nadavtzaysler <110530309+nadavtzaysler@users.noreply.github.com> Date: Thu, 6 Jun 2024 11:18:24 +0300 Subject: [PATCH 2/3] fixed type hinting across all project, moved math functions to different module and fixed behavior/cleancode issues across all code --- .gitignore | 324 +++++++++++++++++++++++----------------------- main.py | 113 +++------------- math_functions.py | 85 ++++++++++++ 3 files changed, 265 insertions(+), 257 deletions(-) create mode 100644 math_functions.py diff --git a/.gitignore b/.gitignore index 82f9275..090fef4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,162 +1,162 @@ -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ -cover/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -.pybuilder/ -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -# For a library or package, you might want to ignore these files since the code is -# intended to run in multiple environments; otherwise, check them in: -# .python-version - -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# poetry -# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. -# This is especially recommended for binary packages to ensure reproducibility, and is more -# commonly ignored for libraries. -# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control -#poetry.lock - -# pdm -# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. -#pdm.lock -# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it -# in version control. -# https://pdm.fming.dev/latest/usage/project/#working-with-version-control -.pdm.toml -.pdm-python -.pdm-build/ - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ - -# pytype static type analyzer -.pytype/ - -# Cython debug symbols -cython_debug/ - -# PyCharm -# JetBrains specific template is maintained in a separate JetBrains.gitignore that can -# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore -# and can be added to the global gitignore or merged into this file. For a more nuclear -# option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/latest/usage/project/#working-with-version-control +.pdm.toml +.pdm-python +.pdm-build/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ diff --git a/main.py b/main.py index 3fe6be1..74573f9 100644 --- a/main.py +++ b/main.py @@ -1,101 +1,24 @@ -import math +from math_functions import print_numbers, calculate_triangular_number, factorial, calculate_power, calculate_square_root, calculate_hypotenuse, calculate_fibonacci_by_index, calculate_factorial_sum, calculate_exponential_sum -def calculate_sum(num: int) -> int: - if num <= 0: - return 0 - return sum(range(num)) - -def is_prime(num: int) -> bool: - for i in range(2, int(math.sqrt(num)) + 1): - if num % i == 0: - return False - return num > 2 - -def factorial(num: int) -> int | None: - if num < 0: - raise ValueError("Negative values are not supported.") - elif num == 0: - return 1 - else: - result = 1 - for i in range(1, num + 1): - result *= i - return result - -def recursive_factorial(num: int) -> int | None: - if num < 0: - raise ValueError("Negative values are not supported.") - elif num == 0 or num == 1: - return 1 - else: - return num * recursive_factorial(num-1) - -def print_numbers(num: int) -> None: - for i in range(num): - if is_prime(i): - print(f"{i} is prime") - else: - print(f"{i} is not prime") - -def calculate_power(base: int, exponent: int) -> int | None: - if exponent < 0: - raise ValueError("Exponent cannot be negative.") - elif exponent == 0: - return 1 - else: - return base ** exponent - -def calculate_square_root(base: int) -> int | float | None: - if base < 0: - raise ValueError("Cannot calculate square root of a negative number.") - else: - return math.sqrt(base) - -def calculate_hypotenuse(a: int, b: int) -> int | None: - if a < 0 or b < 0: - raise ValueError("Both sides must be positive.") - else: - return math.hypot(a, b) - -def calculate_fibonacci(num: int) -> int | None: - if num < 0: - raise ValueError("Negative values are not supported.") - elif num == 0: - return 0 - elif num == 1: - return 1 - else: - a, b = 0, 1 - for _ in range(2, num + 1): - a, b = b, a + b - return b - -def calculate_factorial_sum(num: int) -> int: - if num < 0: - raise ValueError("Negative values are not supported.") - else: - return sum(factorial(i) for i in range(num + 1)) - -def calculate_exponential_sum(exponent: int, num: int) -> int | None: - if num < 0: - raise ValueError("Number of terms cannot be negative.") - else: - return sum(calculate_power(exponent, i) / factorial(i) for i in range(num + 1)) def main(): - try: - num = int(input("Enter a number: ")) - print(f"Sum of numbers from 0 to {num}: {calculate_sum(num)}") - print(f"Factorial of {num}: {factorial(num)}") - print_numbers(num) - print(f"2^3 = {calculate_power(2, 3)}") - print(f"Square root of 16: {calculate_square_root(16)}") - print(f"Hypotenuse of 3 and 4: {calculate_hypotenuse(3, 4)}") - print(f"Fibonacci sequence up to 10: {calculate_fibonacci(10)}") - print(f"Factorial sum up to 5: {calculate_factorial_sum(5)}") - print(f"Exponential sum of 2 up to 5 terms: {calculate_exponential_sum(2, 5)}") - except ValueError as e: - print(f"Error: {e}") + while(True): + try: + num = int(input("Enter a number: ")) + except ValueError as e: + print("please insert positive number") + continue + break + + print(f"Sum of numbers from 0 to {num}: {calculate_triangular_number(num)}") + print(f"Factorial of {num}: {factorial(num)}") + print_numbers(num) + print(f"2^3 = {calculate_power(2, 3)}") + print(f"Square root of 16: {calculate_square_root(16)}") + print(f"Hypotenuse of 3 and 4: {calculate_hypotenuse(3, 4)}") + print(f"Fibonacci sequence up to 10: {calculate_fibonacci_by_index(10)}") + print(f"Factorial sum up to 5: {calculate_factorial_sum(5)}") + print(f"Exponential sum of 2 up to 5 terms: {calculate_exponential_sum(2, 5)}") if __name__ == "__main__": main() \ No newline at end of file diff --git a/math_functions.py b/math_functions.py new file mode 100644 index 0000000..6256e2c --- /dev/null +++ b/math_functions.py @@ -0,0 +1,85 @@ +import math + +def calculate_triangular_number(num: int) -> int: + if num <= 0: + return 0 + return sum(range(num + 1)) + +def is_prime(num): + if num <= 1: + return False + for divisor in range(2, int(math.sqrt(num)) + 1): + if num % divisor == 0: + return False + return True + +def factorial(num: int) -> int: + if num < 0: + raise ValueError("Negative values are not supported.") + elif num == 0: + return 1 + else: + result = 1 + for i in range(1, num + 1): + result *= i + return result + +def recursive_factorial(num: int) -> int: + if num < 0: + raise ValueError("Negative values are not supported.") + elif num == 0 or num == 1: + return 1 + else: + return num * recursive_factorial(num-1) + +def print_numbers(num: int) -> None: + for i in range(num): + if is_prime(i): + print(f"{i} is prime") + else: + print(f"{i} is not prime") + +def calculate_power(base: int, exponent: int) -> int: + if exponent < 0: + raise ValueError("Exponent cannot be negative.") + elif exponent == 0: + return 1 + else: + return base ** exponent + +def calculate_square_root(base: int) -> float: + if base < 0: + raise ValueError("Cannot calculate square root of a negative number.") + else: + return math.sqrt(base) + +def calculate_hypotenuse(a: float, b: float) -> float: + if a < 0 or b < 0: + raise ValueError("Both sides must be positive.") + else: + return math.hypot(a, b) + +def calculate_fibonacci_by_index(index: int) -> int: + if index < 0: + raise ValueError("Negative values are not supported.") + elif index == 0: + return 0 + elif index == 1: + return 1 + else: + a, b = 0, 1 + for _ in range(2, index + 1): + a, b = b, a + b + return b + +def calculate_factorial_sum(num: int) -> int: + if num < 0: + raise ValueError("Negative values are not supported.") + else: + return sum(factorial(i) for i in range(num + 1)) + +def calculate_exponential_sum(exponent: int, num: int) -> int | None: + if num < 0: + raise ValueError("Number of terms cannot be negative.") + else: + return sum(calculate_power(exponent, i) / factorial(i) for i in range(num + 1)) \ No newline at end of file From 328c752c7da3872d62953dfbcf981e7d6aa6d717 Mon Sep 17 00:00:00 2001 From: nadavtzaysler <110530309+nadavtzaysler@users.noreply.github.com> Date: Mon, 10 Jun 2024 14:18:04 +0300 Subject: [PATCH 3/3] Add files via upload --- math_functions.py | 249 ++++++++++++++++++++++++++++++---------------- 1 file changed, 165 insertions(+), 84 deletions(-) diff --git a/math_functions.py b/math_functions.py index 6256e2c..f062734 100644 --- a/math_functions.py +++ b/math_functions.py @@ -1,85 +1,166 @@ -import math - -def calculate_triangular_number(num: int) -> int: - if num <= 0: - return 0 - return sum(range(num + 1)) - -def is_prime(num): - if num <= 1: - return False - for divisor in range(2, int(math.sqrt(num)) + 1): - if num % divisor == 0: - return False - return True - -def factorial(num: int) -> int: - if num < 0: - raise ValueError("Negative values are not supported.") - elif num == 0: - return 1 - else: - result = 1 - for i in range(1, num + 1): - result *= i - return result - -def recursive_factorial(num: int) -> int: - if num < 0: - raise ValueError("Negative values are not supported.") - elif num == 0 or num == 1: - return 1 - else: - return num * recursive_factorial(num-1) - -def print_numbers(num: int) -> None: - for i in range(num): - if is_prime(i): - print(f"{i} is prime") - else: - print(f"{i} is not prime") - -def calculate_power(base: int, exponent: int) -> int: - if exponent < 0: - raise ValueError("Exponent cannot be negative.") - elif exponent == 0: - return 1 - else: - return base ** exponent - -def calculate_square_root(base: int) -> float: - if base < 0: - raise ValueError("Cannot calculate square root of a negative number.") - else: - return math.sqrt(base) - -def calculate_hypotenuse(a: float, b: float) -> float: - if a < 0 or b < 0: - raise ValueError("Both sides must be positive.") - else: - return math.hypot(a, b) - -def calculate_fibonacci_by_index(index: int) -> int: - if index < 0: - raise ValueError("Negative values are not supported.") - elif index == 0: - return 0 - elif index == 1: - return 1 - else: - a, b = 0, 1 - for _ in range(2, index + 1): - a, b = b, a + b - return b - -def calculate_factorial_sum(num: int) -> int: - if num < 0: - raise ValueError("Negative values are not supported.") - else: - return sum(factorial(i) for i in range(num + 1)) - -def calculate_exponential_sum(exponent: int, num: int) -> int | None: - if num < 0: - raise ValueError("Number of terms cannot be negative.") - else: +import math + +def calculate_triangular_number(num: int) -> int: + return sum(range(num + 1)) + +def is_prime(num): + if num <= 1: + return False + for divisor in range(2, int(math.sqrt(num)) + 1): + if num % divisor == 0: + return False + return True + +def factorial(num: int) -> int: + if num < 0: + raise ValueError("Negative values are not supported.") + elif num == 0: + return 1 + else: + result = 1 + for i in range(1, num + 1): + result *= i + return result + +def recursive_factorial(num: int) -> int: + if num < 0: + raise ValueError("Negative values are not supported.") + elif num == 0 or num == 1: + return 1 + else: + return num * recursive_factorial(num-1) + +def print_numbers(num: int) -> None: + for i in range(num): + if is_prime(i): + print(f"{i} is prime") + else: + print(f"{i} is not prime") + +def calculate_power(base: int, exponent: int) -> int: + if exponent < 0: + raise ValueError("Exponent cannot be negative.") + else: + return base ** exponent + +def calculate_square_root(base: int) -> float: + if base < 0: + raise ValueError("Cannot calculate square root of a negative number.") + else: + return math.sqrt(base) + +def calculate_hypotenuse(a: float, b: float) -> float: + if a < 0 or b < 0: + raise ValueError("Both sides must be positive.") + else: + return math.hypot(a, b) + +def calculate_fibonacci_by_index(index: int) -> int: + if index < 0: + raise ValueError("Negative values are not supported.") + elif index == 0: + return 0 + elif index == 1: + return 1 + else: + a, b = 0, 1 + for _ in range(2, index + 1): + a, b = b, a + b + return b + +def calculate_factorial_sum(num: int) -> int: + if num < 0: + raise ValueError("Negative values are not supported.") + else: + return sum(factorial(i) for i in range(num + 1)) + +def calculate_exponential_sum(exponent: int, num: int) -> int | None: + if num < 0: + raise ValueError("Number of terms cannot be negative.") + else: + return sum(calculate_power(exponent, i) / factorial(i) for i in range(num + 1)) +import math + +def calculate_triangular_number(num: int) -> int: + if num <= 0: + return 0 + return sum(range(num + 1)) + +def is_prime(num): + if num <= 1: + return False + for divisor in range(2, int(math.sqrt(num)) + 1): + if num % divisor == 0: + return False + return True + +def factorial(num: int) -> int: + if num < 0: + raise ValueError("Negative values are not supported.") + elif num == 0: + return 1 + else: + result = 1 + for i in range(1, num + 1): + result *= i + return result + +def recursive_factorial(num: int) -> int: + if num < 0: + raise ValueError("Negative values are not supported.") + elif num == 0 or num == 1: + return 1 + else: + return num * recursive_factorial(num-1) + +def print_numbers(num: int) -> None: + for i in range(num): + if is_prime(i): + print(f"{i} is prime") + else: + print(f"{i} is not prime") + +def calculate_power(base: int, exponent: int) -> int: + if exponent < 0: + raise ValueError("Exponent cannot be negative.") + elif exponent == 0: + return 1 + else: + return base ** exponent + +def calculate_square_root(base: int) -> float: + if base < 0: + raise ValueError("Cannot calculate square root of a negative number.") + else: + return math.sqrt(base) + +def calculate_hypotenuse(a: float, b: float) -> float: + if a < 0 or b < 0: + raise ValueError("Both sides must be positive.") + else: + return math.hypot(a, b) + +def calculate_fibonacci_by_index(index: int) -> int: + if index < 0: + raise ValueError("Negative values are not supported.") + elif index == 0: + return 0 + elif index == 1: + return 1 + else: + a, b = 0, 1 + for _ in range(2, index + 1): + a, b = b, a + b + return b + +def calculate_factorial_sum(num: int) -> int: + if num < 0: + raise ValueError("Negative values are not supported.") + else: + return sum(factorial(i) for i in range(num + 1)) + +def calculate_exponential_sum(exponent: int, num: int) -> int | None: + if num < 0: + raise ValueError("Number of terms cannot be negative.") + else: return sum(calculate_power(exponent, i) / factorial(i) for i in range(num + 1)) \ No newline at end of file