From 02b79589b322efb8eb687f831d747d5993c7b470 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 1 Apr 2021 19:48:56 +0200 Subject: Python solution for week 106, part 2 --- challenge-106/abigail/README.md | 1 + challenge-106/abigail/python/ch-2.py | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 challenge-106/abigail/python/ch-2.py diff --git a/challenge-106/abigail/README.md b/challenge-106/abigail/README.md index 8caecfeacd..57eb222c67 100644 --- a/challenge-106/abigail/README.md +++ b/challenge-106/abigail/README.md @@ -98,6 +98,7 @@ Wikipedia](https://en.wikipedia.org/wiki/Repeating_decimal). * [Lua](lua/ch-2.lua) * [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) +* [Python](python/ch-2.py) ### Blog []() 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 ()))) -- cgit