aboutsummaryrefslogtreecommitdiff
path: root/challenge-003/paulo-custodio/python
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-01-28 00:33:56 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-01-28 00:33:56 +0000
commite233d3a019dda275d8d790076d25ec92e47b4ffe (patch)
tree48516daf26833baca8695204a4d83c81d58a6dd7 /challenge-003/paulo-custodio/python
parent022a9f62eda3b5e188b9f6719a63566d9d7efc8b (diff)
downloadperlweeklychallenge-club-e233d3a019dda275d8d790076d25ec92e47b4ffe.tar.gz
perlweeklychallenge-club-e233d3a019dda275d8d790076d25ec92e47b4ffe.tar.bz2
perlweeklychallenge-club-e233d3a019dda275d8d790076d25ec92e47b4ffe.zip
Add Basic, C, C++, Forth, Lua and Python to challenge 003
Diffstat (limited to 'challenge-003/paulo-custodio/python')
-rw-r--r--challenge-003/paulo-custodio/python/ch-1.py36
-rw-r--r--challenge-003/paulo-custodio/python/ch-2.py26
2 files changed, 62 insertions, 0 deletions
diff --git a/challenge-003/paulo-custodio/python/ch-1.py b/challenge-003/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..413228bd57
--- /dev/null
+++ b/challenge-003/paulo-custodio/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+# Challenge 003
+#
+# Challenge #1
+# Create a script to generate 5-smooth numbers, whose prime divisors are less
+# or equal to 5. They are also called Hamming/Regular/Ugly numbers. For more
+# information, please check this wikipedia.
+
+import sys
+
+# return an iterator to generate the sequence
+# the sequence is a merge of all multiples of 2, 3 and 5
+def hamming_gen():
+ seq = [[1], [1], [1]]
+ base = [2, 3, 5]
+
+ while True:
+ # get the smallest of the multiples
+ n = min(seq[0][0], seq[1][0], seq[2][0])
+
+ for i in range(0, 3):
+ # shift used multiples
+ if seq[i][0] == n:
+ seq[i].pop(0)
+
+ # push next multiple
+ seq[i].append(n*base[i])
+
+ yield n
+
+
+# main
+iter = hamming_gen()
+for i in range(0, int(sys.argv[1])):
+ print(next(iter))
diff --git a/challenge-003/paulo-custodio/python/ch-2.py b/challenge-003/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..d97750c634
--- /dev/null
+++ b/challenge-003/paulo-custodio/python/ch-2.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+
+# Challenge 003
+#
+# Challenge #2
+# Create a script that generates Pascal Triangle. Accept number of rows from
+# the command line. The Pascal Triangle should have at least 3 rows. For more
+# information about Pascal Triangle, check this wikipedia page.
+
+import sys
+
+def draw_pascal(rows):
+ data = [1]
+ for row in range(1,rows+1):
+ # print current row
+ print(" "*(rows-row) + " ".join([str(x) for x in data]))
+
+ # compute next row
+ nxt = [1]
+ for col in range(0, len(data)-1):
+ nxt.append(data[col] + data[col+1])
+ nxt.append(1)
+ data = nxt
+
+# main
+draw_pascal(int(sys.argv[1]))