diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-12 18:56:29 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-12 18:56:29 +0100 |
| commit | 49f7f459092f538b5468e255ff4e8ebac490d70a (patch) | |
| tree | fa4a94a8207a58a56d4dca98ff22d39bed047f6e /challenge-159/lubos-kolouch/python/ch-1.py | |
| parent | 1711da4f548d3134248cd5e02a13d71762d5e4cb (diff) | |
| parent | 72ba70a96cfd587443c3eaa0f5ba02e3557f4b82 (diff) | |
| download | perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.tar.gz perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.tar.bz2 perlweeklychallenge-club-49f7f459092f538b5468e255ff4e8ebac490d70a.zip | |
Merge remote-tracking branch 'upstream/master'
# Conflicts:
# challenge-160/paulo-custodio/Makefile
Diffstat (limited to 'challenge-159/lubos-kolouch/python/ch-1.py')
| -rw-r--r-- | challenge-159/lubos-kolouch/python/ch-1.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/challenge-159/lubos-kolouch/python/ch-1.py b/challenge-159/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..cdfcf94d85 --- /dev/null +++ b/challenge-159/lubos-kolouch/python/ch-1.py @@ -0,0 +1,20 @@ +""" Challenge 159 Task 1""" + + +def farey_sequence(n: int, descending: bool = False) -> str: + """Print the n'th Farey sequence. Allow for either ascending or descending.""" + (a, b, c, d) = (0, 1, 1, n) + + solution = [] + if descending: + (a, c) = (1, n - 1) + solution.append(f"{a}/{b}") + while (c <= n and not descending) or (a > 0 and descending): + k = (n + b) // d + (a, b, c, d) = (c, d, k * c - a, k * d - b) + solution.append(f"{a}/{b}") + + return ", ".join(solution) + + +assert farey_sequence(5) == "0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1" |
