diff options
| -rw-r--r-- | challenge-043/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-043/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-043/paulo-custodio/perl/ch-1.pl | 51 | ||||
| -rw-r--r-- | challenge-043/paulo-custodio/perl/ch-2.pl | 58 | ||||
| -rw-r--r-- | challenge-043/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-043/paulo-custodio/t/test-2.yaml | 25 |
6 files changed, 142 insertions, 0 deletions
diff --git a/challenge-043/paulo-custodio/Makefile b/challenge-043/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-043/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-043/paulo-custodio/README b/challenge-043/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-043/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-043/paulo-custodio/perl/ch-1.pl b/challenge-043/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..787812b2ef --- /dev/null +++ b/challenge-043/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/usr/bin/env perl + +# 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. + +use Modern::Perl; + +my $total = 11; + +my $red = 9; +my $red_green = $total-$red; + +my $green = 5; +my $green_black = $total-$green-$red_green; + +my $blue = 8; +my $blue_yellow = $total-$blue; + +my $yellow = 7; +my $yellow_black = $total-$yellow-$blue_yellow; + +my $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); + +say "$red $red_green $green $green_black $black $yellow_black $yellow $blue_yellow $blue"; + +sub assert { + my($f) = @_; + $f or die "assertion failed"; +} diff --git a/challenge-043/paulo-custodio/perl/ch-2.pl b/challenge-043/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..4295a76046 --- /dev/null +++ b/challenge-043/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl + +# 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. + +use Modern::Perl; + +my $base = shift||4; +print_self_descriptive($base); + +sub print_self_descriptive { + my($base) = @_; + my @n = (1, (0) x ($base-1)); + while (@n == $base) { + if (is_self_descriptive(@n)) { + say join '', @n; + return; + } + increment(\@n, $base); + } +} + +sub increment { + my($n, $base) = @_; + my $i = $#$n; + while ($i >= 0) { + $n->[$i]++; + if ($n->[$i] < $base) { + return; + } + else { + $n->[$i] = 0; + $i--; + } + } + unshift @$n, 1; +} + +sub is_self_descriptive { + my(@n) = @_; + for my $i (0..$#n) { + my $count = scalar grep {$_==$i} @n; + return if $n[$i] != $count; + } + return 1; +} diff --git a/challenge-043/paulo-custodio/t/test-1.yaml b/challenge-043/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..a7f06c1429 --- /dev/null +++ b/challenge-043/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: + input: + output: 9 2 5 4 6 1 7 3 8 diff --git a/challenge-043/paulo-custodio/t/test-2.yaml b/challenge-043/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..812c38975b --- /dev/null +++ b/challenge-043/paulo-custodio/t/test-2.yaml @@ -0,0 +1,25 @@ +- setup: + cleanup: + args: 4 + input: + output: 1210 +- setup: + cleanup: + args: 5 + input: + output: 21200 +- setup: + cleanup: + args: 7 + input: + output: 3211000 +- setup: + cleanup: + args: 8 + input: + output: 42101000 +- setup: + cleanup: + args: 9 + input: + output: 521001000 |
