From d918cad0878f4ab9c170f194ac5b72c97ad2f64f Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 28 Apr 2022 16:27:29 +0100 Subject: Add Python solution to challenge 043 --- challenge-043/paulo-custodio/python/ch-1.py | 44 +++++++++++++++++++++++++++ challenge-043/paulo-custodio/python/ch-2.py | 47 +++++++++++++++++++++++++++++ challenge-043/paulo-custodio/t/test-2.yaml | 5 --- 3 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 challenge-043/paulo-custodio/python/ch-1.py create mode 100644 challenge-043/paulo-custodio/python/ch-2.py 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 -- cgit