|
| 1 | +def gcd(numerator: int, denominator: int) -> int: |
| 2 | + """ |
| 3 | + >>> gcd(12, 18) |
| 4 | + 6 |
| 5 | + >>> gcd(20, 25) |
| 6 | + 5 |
| 7 | + >>> gcd(20, 0) |
| 8 | + Traceback (most recent call last): |
| 9 | + ValueError: The Denominator cannot be 0 |
| 10 | + """ |
| 11 | + if denominator == 0: |
| 12 | + raise ValueError("The Denominator cannot be 0") |
| 13 | + while denominator: |
| 14 | + numerator, denominator = denominator, numerator % denominator |
| 15 | + return numerator |
| 16 | + |
| 17 | + |
| 18 | +def proper_fractions(denominator: int) -> list[str]: |
| 19 | + """ |
| 20 | + this algorithm returns a list of proper fractions, in the |
| 21 | + range between 0 and 1, which can be formed with the given denominator |
| 22 | + >>> proper_fractions(10) |
| 23 | + ['1/10', '3/10', '7/10', '9/10'] |
| 24 | + >>> proper_fractions(5) |
| 25 | + ['1/5', '2/5', '3/5', '4/5'] |
| 26 | + >>> proper_fractions(-15) |
| 27 | + Traceback (most recent call last): |
| 28 | + ValueError: The Denominator Cannot be less than 0 |
| 29 | + >>> |
| 30 | + """ |
| 31 | + |
| 32 | + if denominator < 0: |
| 33 | + raise ValueError("The Denominator Cannot be less than 0") |
| 34 | + fractions: list[str] = [] |
| 35 | + for numerator in range(1, denominator): |
| 36 | + if gcd(numerator, denominator) == 1: |
| 37 | + fractions.append(f"{numerator}/{denominator}") |
| 38 | + return fractions |
| 39 | + |
| 40 | + |
| 41 | +if __name__ == "__main__": |
| 42 | + __import__("doctest").testmod() |
0 commit comments