aboutsummaryrefslogtreecommitdiff
path: root/challenge-003/abigail/python/ch-1.py
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-01-09 06:04:01 +0000
committerdrbaggy <js5@sanger.ac.uk>2022-01-09 06:04:01 +0000
commit29b9eeeb95555dbcf1f375c89910c83ac83abd8d (patch)
tree857c2d4c063b88bedcebd76079cf488df977b330 /challenge-003/abigail/python/ch-1.py
parentd933e4e040eae5d2d4d69b6b4da2d312cd4887e4 (diff)
parentf2e062cd585f30ecbcf0257e72ccb8f0c82136a7 (diff)
downloadperlweeklychallenge-club-29b9eeeb95555dbcf1f375c89910c83ac83abd8d.tar.gz
perlweeklychallenge-club-29b9eeeb95555dbcf1f375c89910c83ac83abd8d.tar.bz2
perlweeklychallenge-club-29b9eeeb95555dbcf1f375c89910c83ac83abd8d.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-003/abigail/python/ch-1.py')
-rw-r--r--challenge-003/abigail/python/ch-1.py40
1 files changed, 23 insertions, 17 deletions
diff --git a/challenge-003/abigail/python/ch-1.py b/challenge-003/abigail/python/ch-1.py
index 30b41dd8fe..8b3ac1e035 100644
--- a/challenge-003/abigail/python/ch-1.py
+++ b/challenge-003/abigail/python/ch-1.py
@@ -1,27 +1,33 @@
#!/opt/local/bin/python
#
-# See ../READ.md
+# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003
#
#
-# Run as python ch-1.py < input-file
+# Run as: python ch-1.py < input-file
#
import fileinput
-for line in fileinput . input ():
- max = int (line)
- #
- # Python does not have a for (;;) style loop
- #
- base2 = 1
- while base2 <= max:
- base3 = base2
- while base3 <= max:
- base5 = base3
- while base5 <= max:
- print base5
- base5 *= 5
- base3 *= 3
- base2 *= 2
+ugly = [1]
+next_2 = 0
+next_3 = 0
+next_5 = 0
+
+
+for n in fileinput . input ():
+ n = int (n)
+ while len (ugly) < n:
+ ugly . append (min ([2 * ugly [next_2],
+ 3 * ugly [next_3],
+ 5 * ugly [next_5]]))
+
+ if 2 * ugly [next_2] <= ugly [-1]:
+ next_2 = next_2 + 1
+ if 3 * ugly [next_3] <= ugly [-1]:
+ next_3 = next_3 + 1
+ if 5 * ugly [next_5] <= ugly [-1]:
+ next_5 = next_5 + 1
+
+ print (ugly [n - 1])