aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-04-01 19:48:56 +0200
committerAbigail <abigail@abigail.be>2021-04-01 19:48:56 +0200
commit02b79589b322efb8eb687f831d747d5993c7b470 (patch)
treecfa6a4ca8332bec56d04714b6786bc0c4255a974
parentdb05bd869a464e6656e759c6f422717175eebcc8 (diff)
downloadperlweeklychallenge-club-02b79589b322efb8eb687f831d747d5993c7b470.tar.gz
perlweeklychallenge-club-02b79589b322efb8eb687f831d747d5993c7b470.tar.bz2
perlweeklychallenge-club-02b79589b322efb8eb687f831d747d5993c7b470.zip
Python solution for week 106, part 2
-rw-r--r--challenge-106/abigail/README.md1
-rw-r--r--challenge-106/abigail/python/ch-2.py37
2 files changed, 38 insertions, 0 deletions
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 ())))