diff options
Diffstat (limited to 'challenge-106/abigail/python/ch-2.py')
| -rw-r--r-- | challenge-106/abigail/python/ch-2.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/challenge-106/abigail/python/ch-2.py b/challenge-106/abigail/python/ch-2.py new file mode 100644 index 0000000000..4182105abf --- /dev/null +++ b/challenge-106/abigail/python/ch-2.py @@ -0,0 +1,37 @@ +#!/opt/local/bin/python + +# +# See ../README.md +# + +# +# Run as: python ch-2.py < input-file +# + +import fileinput + +# +# See ../README.md for description of the how the method works +# +def long_division (numerator, denominator): + BASE = 10 + fraction = str (numerator // denominator) + "." + position = len (fraction) + seen = {} + + numerator = numerator % denominator + + while not (numerator in seen): + if numerator == 0: + return (fraction) + seen [numerator] = position + fraction = fraction + str (BASE * numerator // denominator) + numerator = BASE * numerator % denominator + position = position + 1 + + return (fraction [:seen [numerator]] + "(" + + fraction [ seen [numerator]:] + ")") + + +for line in fileinput . input (): + print (long_division (*map (lambda _: int (_), line . split ()))) |
