aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-03-30 02:39:42 +0100
committerGitHub <noreply@github.com>2021-03-30 02:39:42 +0100
commit5d20a484ac4e9d0b7c0e048ed973084329d555de (patch)
tree89d6b7a554c2816ffe08f701525b9e8ba7a3924f
parent7e2503f51659c7d8f0479bc7e010b15154862b02 (diff)
parent25679b350895b4d537762b038bfbebd4879681c7 (diff)
downloadperlweeklychallenge-club-5d20a484ac4e9d0b7c0e048ed973084329d555de.tar.gz
perlweeklychallenge-club-5d20a484ac4e9d0b7c0e048ed973084329d555de.tar.bz2
perlweeklychallenge-club-5d20a484ac4e9d0b7c0e048ed973084329d555de.zip
Merge pull request #3803 from stuart-little/stuart-little_106_python
1st commit on 106_python
-rwxr-xr-xchallenge-106/stuart-little/python/ch-1.py10
-rwxr-xr-xchallenge-106/stuart-little/python/ch-2.py33
2 files changed, 43 insertions, 0 deletions
diff --git a/challenge-106/stuart-little/python/ch-1.py b/challenge-106/stuart-little/python/ch-1.py
new file mode 100755
index 0000000000..2a63047df2
--- /dev/null
+++ b/challenge-106/stuart-little/python/ch-1.py
@@ -0,0 +1,10 @@
+#!/usr/bin/env python
+
+# run <script> <space-separated numbers>
+
+import sys
+
+srt = sorted(map(lambda x: int(x), sys.argv[1:]))
+diffs = [y-x for (x,y) in zip(srt,srt[1:])]
+
+print(max(diffs) if len(diffs)>0 else 0)
diff --git a/challenge-106/stuart-little/python/ch-2.py b/challenge-106/stuart-little/python/ch-2.py
new file mode 100755
index 0000000000..279757366a
--- /dev/null
+++ b/challenge-106/stuart-little/python/ch-2.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+
+# run <script> <numerator> <denominator>
+
+import sys
+
+def maxExp(p,n):
+ return 0 if (n % p) else (1+maxExp(p, n//p))
+
+def ordExp(n,p):
+ (order,base)=(1,n % p)
+ while (base-1 % p):
+ order += 1
+ base = (base * n) % p
+ return order
+
+def when10copr(num,den):
+ intg = num//den
+ digs = ordExp(10,den)
+ fract = ((num % den) * (10**digs-1)) // den
+ return intg, '0' * (digs - len(str(fract))) + str(fract)
+
+def fractPart(num,den):
+ exp2 = maxExp(2,den)
+ exp5 = maxExp(5,den);
+ newNum = (5**(exp2-exp5) * num) if (exp2 >= exp5) else (2**(exp5-exp2) * num)
+ copr = when10copr(newNum, den // (2**exp2 * 5**exp5))
+ nonRep = ('0' * (max(exp2,exp5) - len(str(copr[0]))) + str(copr[0])) if (copr[0] or max(exp2,exp5)) else ""
+ rep = f"({copr[1]})" if (copr[1] != "0") else ""
+ return nonRep + rep
+
+(num,den) = list(map(lambda x: int(x), sys.argv[1:3]))
+print(f"{num//den}.{fractPart(num % den,den)}")