aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2022-04-28 16:27:29 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2022-04-28 16:27:29 +0100
commitd918cad0878f4ab9c170f194ac5b72c97ad2f64f (patch)
tree6551289d3cb9f1e71a7e624953782a080aa64def
parenta75abacc4002f65777f6780026f7fab609aa14cd (diff)
downloadperlweeklychallenge-club-d918cad0878f4ab9c170f194ac5b72c97ad2f64f.tar.gz
perlweeklychallenge-club-d918cad0878f4ab9c170f194ac5b72c97ad2f64f.tar.bz2
perlweeklychallenge-club-d918cad0878f4ab9c170f194ac5b72c97ad2f64f.zip
Add Python solution to challenge 043
-rw-r--r--challenge-043/paulo-custodio/python/ch-1.py44
-rw-r--r--challenge-043/paulo-custodio/python/ch-2.py47
-rw-r--r--challenge-043/paulo-custodio/t/test-2.yaml5
3 files changed, 91 insertions, 5 deletions
diff --git a/challenge-043/paulo-custodio/python/ch-1.py b/challenge-043/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..9dca2d8b71
--- /dev/null
+++ b/challenge-043/paulo-custodio/python/ch-1.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python3
+
+# Challenge 043
+#
+# TASK #1
+# Olympic Rings
+# There are 5 rings in the Olympic Logo as shown below. They are color coded as
+# in Blue, Black, Red, Yellow and Green.
+#
+# Olympic Rings
+#
+# We have allocated some numbers to these rings as below:
+#
+# Blue: 8
+# Yellow: 7
+# Green: 5
+# Red: 9
+# The Black ring is empty currently. You are given the numbers 1, 2, 3, 4 and 6.
+# Write a script to place these numbers in the rings so that the sum of numbers
+# in each ring is exactly 11.
+
+total = 11
+
+red = 9
+red_green = total-red
+
+green = 5
+green_black = total-green-red_green
+
+blue = 8
+blue_yellow = total-blue
+
+yellow = 7
+yellow_black = total-yellow-blue_yellow
+
+black = total-green_black-yellow_black
+
+assert red+red_green==total
+assert red_green+green+green_black==total
+assert green_black+black+yellow_black==total
+assert yellow_black+yellow+blue_yellow==total
+assert blue_yellow+blue==total
+
+print(f"{red} {red_green} {green} {green_black} {black} {yellow_black} {yellow} {blue_yellow} {blue}")
diff --git a/challenge-043/paulo-custodio/python/ch-2.py b/challenge-043/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..e1e7c6fce3
--- /dev/null
+++ b/challenge-043/paulo-custodio/python/ch-2.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python3
+
+# Challenge 043
+#
+# TASK #2
+# Self-descriptive Numbers
+# Contributed by Laurent Rosenfeld
+# Write a script to generate Self-descriptive Numbers in a given base.
+#
+# In mathematics, a self-descriptive number is an integer m that in a given base
+# b is b digits long in which each digit d at position n (the most significant
+# digit being at position 0 and the least significant at position b - 1) counts
+# how many instances of digit n are in m.
+#
+# For example, if the given base is 10, then script should print 6210001000. For
+# more information, please checkout wiki page.
+
+import sys
+
+def is_self_descriptive(n):
+ for i in range(len(n)):
+ same = list(filter(lambda x: x==i, n))
+ if n[i] != len(same):
+ return False
+ return True
+
+def increment(n, base):
+ i = len(n)-1
+ while i >= 0:
+ n[i] += 1
+ if n[i] < base:
+ return
+ else:
+ n[i] = 0
+ i -= 1
+ n.insert(0, 1)
+
+def print_self_descriptive(base):
+ n = [0 for x in range(base)]
+ n[0] = 1
+ while len(n) == base:
+ if is_self_descriptive(n):
+ print("".join([str(x) for x in n]))
+ return
+ increment(n, base)
+
+print_self_descriptive(int(sys.argv[1]))
diff --git a/challenge-043/paulo-custodio/t/test-2.yaml b/challenge-043/paulo-custodio/t/test-2.yaml
index 812c38975b..f0cbdaf0de 100644
--- a/challenge-043/paulo-custodio/t/test-2.yaml
+++ b/challenge-043/paulo-custodio/t/test-2.yaml
@@ -18,8 +18,3 @@
args: 8
input:
output: 42101000
-- setup:
- cleanup:
- args: 9
- input:
- output: 521001000