aboutsummaryrefslogtreecommitdiff
path: root/challenge-159/eric-cheung/python/ch-1.py
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-04-11 19:25:14 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-04-11 19:25:14 +0100
commit6e220136f3cd0b6189d3ea501127ae7b8531b0c6 (patch)
treeadc79db5a1d29b65c8a530d4f40dc72c12c1d2b4 /challenge-159/eric-cheung/python/ch-1.py
parentfb9997d2e0f2970452265eff24e412f0a4588143 (diff)
downloadperlweeklychallenge-club-6e220136f3cd0b6189d3ea501127ae7b8531b0c6.tar.gz
perlweeklychallenge-club-6e220136f3cd0b6189d3ea501127ae7b8531b0c6.tar.bz2
perlweeklychallenge-club-6e220136f3cd0b6189d3ea501127ae7b8531b0c6.zip
- Added guest contributions by Eric Cheung.
Diffstat (limited to 'challenge-159/eric-cheung/python/ch-1.py')
-rwxr-xr-xchallenge-159/eric-cheung/python/ch-1.py98
1 files changed, 52 insertions, 46 deletions
diff --git a/challenge-159/eric-cheung/python/ch-1.py b/challenge-159/eric-cheung/python/ch-1.py
index 84e4d54b07..82de733403 100755
--- a/challenge-159/eric-cheung/python/ch-1.py
+++ b/challenge-159/eric-cheung/python/ch-1.py
@@ -1,65 +1,71 @@
## Remarks
-## https://www.geeksforgeeks.org/farey-sequence/
+## https://rosettacode.org/wiki/Four_is_magic#Python
-## Python3 program to print
-## Farey Sequence of given order
+from collections import OrderedDict
+
+numbers = {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine'}
-## Class for fraction x / y (a term in farey sequence)
-class Term:
+numbers = OrderedDict(sorted(numbers.items(), key = lambda t: t[0], reverse = True))
- ## Constructor to initialize
- ## x and y in x / y
- def __init__(self, x, y):
- self.x = x
- self.y = y
+def string_representation(i: int) -> str:
+ """
+ Return the english string representation of an integer
+ """
+ if i == 0:
+ return "zero"
+ words = ['negative'] if i < 0 else []
-## GCD of a and b
-def gcd(a, b):
- if b == 0:
- return a
+ working_copy = abs(i)
- return gcd(b, a % b)
+ for key, value in numbers.items():
+ if key <= working_copy:
+ times = int(working_copy / key)
+
+ if key >= 100:
+ words.append(string_representation(times))
-## Function to print
-## Farey sequence of order n
-def farey(n):
+ words.append(value)
+ working_copy -= times * key
- ## Create a vector to store terms of output
- v = []
+ if working_copy == 0:
+ break
+
+ return " ".join(words)
- ## One by one find and store all terms except 0 / 1 and n / n which are known
- for i in range(1, n + 1):
- for j in range(i + 1, n + 1):
- ## Checking whether i and j are in lowest term
- if gcd(i, j) == 1:
- v.append(Term(i, j))
+def next_phrase(i: int):
+ """
+ Generate all the phrases
+ """
+ while not i == 4: # Generate phrases until four is reached
+ str_i = string_representation(i)
+ len_i = len(str_i)
- ## Sorting the term of sequence
- for i in range(len(v)):
- for j in range(i + 1, len(v)):
- if (v[i].x * v[j].y > v[j].x * v[i].y):
- v[i], v[j] = v[j], v[i]
+ yield str_i, "is", string_representation(len_i)
- ## Explicitly printing first term
- print("0 / 1", end = " ")
+ i = len_i
+
+ ## The Last Phrase
+ yield string_representation(i), "is", "magic"
+
+
+def magic(i: int) -> str:
+ phrases = []
- ## Printing other terms
- for i in range(len(v)):
- print("%d/%d" % (v[i].x, v[i].y), end = " ")
+ for phrase in next_phrase(i):
+ phrases.append(" ".join(phrase))
- ## explicitly printing last term
- print("1/1")
+ return f'{", ".join(phrases)}.'.capitalize()
-# Driver Code
-if __name__ == "__main__":
- ## n = 5 ## Example 1:
- ## n = 7 ## Example 2:
- n = 4 ## Example 3:
+## Driver Code
+
+## nInput = 5 ## Example 1:
+## nInput = 7 ## Example 2:
+nInput = 6 ## Example 3:
+
+strMsg = magic(nInput)
- print("Farey sequence of order %d is" % n)
- farey(n)
+print (strMsg)
-## This code is contributed by sanjeev2552