From 8c6fe270e92d485c6dadb3ef61b281bfa0fc3899 Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 27 Dec 2023 17:18:29 +0200 Subject: [PATCH 1/9] adding a proper fractions algorithm --- maths/numerical_analysis/proper_fractions.py | 42 ++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 maths/numerical_analysis/proper_fractions.py diff --git a/maths/numerical_analysis/proper_fractions.py b/maths/numerical_analysis/proper_fractions.py new file mode 100644 index 000000000000..21ed90345d35 --- /dev/null +++ b/maths/numerical_analysis/proper_fractions.py @@ -0,0 +1,42 @@ +def gcd(numerator: int, denominator: int) -> int: + """ + >>> gcd(12, 18) + 6 + >>> gcd(20, 25) + 5 + >>> gcd(20, 0) + Traceback (most recent call last): + ValueError: The Denominator cannot be 0 + """ + if denominator == 0: + raise ValueError("The Denominator cannot be 0") + while denominator: + numerator, denominator = denominator, numerator % denominator + return numerator + + +def proper_fractions(denominator: int) -> list[str]: + """ + this algorithm returns a list of proper fractions, in the + range between 0 and 1, which can be formed with the given denominator + >>> proper_fractions(10) + ['1/10', '3/10', '7/10', '9/10'] + >>> proper_fractions(5) + ['1/5', '2/5', '3/5', '4/5'] + >>> proper_fractions(-15) + Traceback (most recent call last): + ValueError: The Denominator Cannot be less than 0 + >>> + """ + + if denominator < 0: + raise ValueError("The Denominator Cannot be less than 0") + fractions: list[str] = [] + for numerator in range(1, denominator): + if gcd(numerator, denominator) == 1: + fractions.append(f"{numerator}/{denominator}") + return fractions + + +if __name__ == "__main__": + __import__("doctest").testmod() From 298768a4a83e15d9c390571e9595b0fc9aa0b59e Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 20 Mar 2024 13:50:19 +0200 Subject: [PATCH 2/9] Implementing suggestions in maths/numerical_analysis/proper_fractions.py Co-authored-by: Christian Clauss --- maths/numerical_analysis/proper_fractions.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/maths/numerical_analysis/proper_fractions.py b/maths/numerical_analysis/proper_fractions.py index 21ed90345d35..4719a62bf10c 100644 --- a/maths/numerical_analysis/proper_fractions.py +++ b/maths/numerical_analysis/proper_fractions.py @@ -31,11 +31,7 @@ def proper_fractions(denominator: int) -> list[str]: if denominator < 0: raise ValueError("The Denominator Cannot be less than 0") - fractions: list[str] = [] - for numerator in range(1, denominator): - if gcd(numerator, denominator) == 1: - fractions.append(f"{numerator}/{denominator}") - return fractions + return [f"{numerator}/{denominator}" for numerator in range(1, denominator) if gcd(numerator, denominator) == 1] if __name__ == "__main__": From b6f624cc57d52eac543444a0146007c7e61ab8c7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 20 Mar 2024 11:50:46 +0000 Subject: [PATCH 3/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/numerical_analysis/proper_fractions.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/maths/numerical_analysis/proper_fractions.py b/maths/numerical_analysis/proper_fractions.py index 4719a62bf10c..c29b8eaefc9d 100644 --- a/maths/numerical_analysis/proper_fractions.py +++ b/maths/numerical_analysis/proper_fractions.py @@ -31,7 +31,11 @@ def proper_fractions(denominator: int) -> list[str]: if denominator < 0: raise ValueError("The Denominator Cannot be less than 0") - return [f"{numerator}/{denominator}" for numerator in range(1, denominator) if gcd(numerator, denominator) == 1] + return [ + f"{numerator}/{denominator}" + for numerator in range(1, denominator) + if gcd(numerator, denominator) == 1 + ] if __name__ == "__main__": From a01f1006e78513c2d35d2aedff7d5a1416581e81 Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 20 Mar 2024 14:06:45 +0200 Subject: [PATCH 4/9] Implementing suggestions to proper_fractions.py --- maths/numerical_analysis/proper_fractions.py | 29 ++++++++------------ 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/maths/numerical_analysis/proper_fractions.py b/maths/numerical_analysis/proper_fractions.py index c29b8eaefc9d..22c01ad73617 100644 --- a/maths/numerical_analysis/proper_fractions.py +++ b/maths/numerical_analysis/proper_fractions.py @@ -1,24 +1,13 @@ -def gcd(numerator: int, denominator: int) -> int: - """ - >>> gcd(12, 18) - 6 - >>> gcd(20, 25) - 5 - >>> gcd(20, 0) - Traceback (most recent call last): - ValueError: The Denominator cannot be 0 - """ - if denominator == 0: - raise ValueError("The Denominator cannot be 0") - while denominator: - numerator, denominator = denominator, numerator % denominator - return numerator +from math import gcd +import doctest def proper_fractions(denominator: int) -> list[str]: """ this algorithm returns a list of proper fractions, in the range between 0 and 1, which can be formed with the given denominator + proper fractions: https://en.wikipedia.org/wiki/Fraction#:~:text=Proper%20and%20improper,and%203/3. + >>> proper_fractions(10) ['1/10', '3/10', '7/10', '9/10'] >>> proper_fractions(5) @@ -26,11 +15,17 @@ def proper_fractions(denominator: int) -> list[str]: >>> proper_fractions(-15) Traceback (most recent call last): ValueError: The Denominator Cannot be less than 0 - >>> + >>> proper_fractions(0) + [] + >>> proper_fractions(1.2) + Traceback (most recent call last): + ValueError: The Denominator has to be an integer """ if denominator < 0: raise ValueError("The Denominator Cannot be less than 0") + elif isinstance(denominator, float): + raise ValueError("The Denominator has to be an integer") return [ f"{numerator}/{denominator}" for numerator in range(1, denominator) @@ -39,4 +34,4 @@ def proper_fractions(denominator: int) -> list[str]: if __name__ == "__main__": - __import__("doctest").testmod() + doctest.testmod() From f71b575a77262765f46acd320ab4a480489b105a Mon Sep 17 00:00:00 2001 From: Margaret <62753112+meg-1@users.noreply.github.com> Date: Wed, 20 Mar 2024 14:12:40 +0200 Subject: [PATCH 5/9] Fixing ruff errors in proper_fractions.py --- maths/numerical_analysis/proper_fractions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/numerical_analysis/proper_fractions.py b/maths/numerical_analysis/proper_fractions.py index 22c01ad73617..8ca3ada15569 100644 --- a/maths/numerical_analysis/proper_fractions.py +++ b/maths/numerical_analysis/proper_fractions.py @@ -1,5 +1,5 @@ -from math import gcd import doctest +from math import gcd def proper_fractions(denominator: int) -> list[str]: From 24134682fb599b18c2217777e87437baf1c5ba98 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 20 Mar 2024 14:18:57 +0100 Subject: [PATCH 6/9] Apply suggestions from code review --- maths/numerical_analysis/proper_fractions.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/maths/numerical_analysis/proper_fractions.py b/maths/numerical_analysis/proper_fractions.py index 8ca3ada15569..01d5470505b4 100644 --- a/maths/numerical_analysis/proper_fractions.py +++ b/maths/numerical_analysis/proper_fractions.py @@ -1,4 +1,3 @@ -import doctest from math import gcd @@ -6,7 +5,7 @@ def proper_fractions(denominator: int) -> list[str]: """ this algorithm returns a list of proper fractions, in the range between 0 and 1, which can be formed with the given denominator - proper fractions: https://en.wikipedia.org/wiki/Fraction#:~:text=Proper%20and%20improper,and%203/3. + https://en.wikipedia.org/wiki/Fraction#Proper_and_improper_fractions >>> proper_fractions(10) ['1/10', '3/10', '7/10', '9/10'] @@ -25,7 +24,7 @@ def proper_fractions(denominator: int) -> list[str]: if denominator < 0: raise ValueError("The Denominator Cannot be less than 0") elif isinstance(denominator, float): - raise ValueError("The Denominator has to be an integer") + raise ValueError("The Denominator must be an integer") return [ f"{numerator}/{denominator}" for numerator in range(1, denominator) From 0c65134965d9c865cc65270213df9327485a7c63 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 20 Mar 2024 14:20:25 +0100 Subject: [PATCH 7/9] ruff check --output-format=github . --- .github/workflows/ruff.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ruff.yml b/.github/workflows/ruff.yml index 496f1460e074..e2fb20a02132 100644 --- a/.github/workflows/ruff.yml +++ b/.github/workflows/ruff.yml @@ -13,4 +13,4 @@ jobs: steps: - uses: actions/checkout@v4 - run: pip install --user ruff - - run: ruff --output-format=github . + - run: ruff check --output-format=github . From 9ee43a063af2145be4b2f5f397117db28b9987e4 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 20 Mar 2024 14:21:23 +0100 Subject: [PATCH 8/9] Update maths/numerical_analysis/proper_fractions.py --- maths/numerical_analysis/proper_fractions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/maths/numerical_analysis/proper_fractions.py b/maths/numerical_analysis/proper_fractions.py index 01d5470505b4..f2abc90d7ecb 100644 --- a/maths/numerical_analysis/proper_fractions.py +++ b/maths/numerical_analysis/proper_fractions.py @@ -33,4 +33,6 @@ def proper_fractions(denominator: int) -> list[str]: if __name__ == "__main__": - doctest.testmod() + from doctest import testmod + + testmod() From a46940e76f85a16c773de554bb83e1bfd83e9198 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 20 Mar 2024 14:26:31 +0100 Subject: [PATCH 9/9] Update proper_fractions.py --- maths/numerical_analysis/proper_fractions.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/maths/numerical_analysis/proper_fractions.py b/maths/numerical_analysis/proper_fractions.py index f2abc90d7ecb..774ce9a24876 100644 --- a/maths/numerical_analysis/proper_fractions.py +++ b/maths/numerical_analysis/proper_fractions.py @@ -13,12 +13,14 @@ def proper_fractions(denominator: int) -> list[str]: ['1/5', '2/5', '3/5', '4/5'] >>> proper_fractions(-15) Traceback (most recent call last): + ... ValueError: The Denominator Cannot be less than 0 >>> proper_fractions(0) [] >>> proper_fractions(1.2) Traceback (most recent call last): - ValueError: The Denominator has to be an integer + ... + ValueError: The Denominator must be an integer """ if denominator < 0: