diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-01-16 09:27:34 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-16 09:27:34 +0000 |
| commit | ee67977cb417ab3c14ba09e6b39cf3f9292bd50c (patch) | |
| tree | fd210fc8ec3c3ed60637e16e499caadadb735e5a /challenge-043 | |
| parent | 61bf45cc4a6a6282d910ecbb6b3ab1fa80145b1b (diff) | |
| parent | 661f8b19d611bfc41adeb3af37a78b4a377b6ead (diff) | |
| download | perlweeklychallenge-club-ee67977cb417ab3c14ba09e6b39cf3f9292bd50c.tar.gz perlweeklychallenge-club-ee67977cb417ab3c14ba09e6b39cf3f9292bd50c.tar.bz2 perlweeklychallenge-club-ee67977cb417ab3c14ba09e6b39cf3f9292bd50c.zip | |
Merge pull request #1140 from andrezgz/challenge-043
challenge-043 andrezgz solution
Diffstat (limited to 'challenge-043')
| -rw-r--r-- | challenge-043/andrezgz/perl/ch-1.pl | 62 | ||||
| -rw-r--r-- | challenge-043/andrezgz/perl/ch-2.pl | 51 |
2 files changed, 113 insertions, 0 deletions
diff --git a/challenge-043/andrezgz/perl/ch-1.pl b/challenge-043/andrezgz/perl/ch-1.pl new file mode 100644 index 0000000000..ee8a90b293 --- /dev/null +++ b/challenge-043/andrezgz/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-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. +# +# 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 strict; +use warnings; + +my %values = ( + Red => 9, + Green => 5, + Yellow => 7, + Blue => 8, +); + +my %numbers = map {$_ => 1} (1,2,3,4,6); + +# each ring components with unknown value at the end +my @rings_components = ( + ['Red','RedGreen'], + ['Green','RedGreen','GreenBlack'], + ['Blue','YellowBlue'], + ['Yellow','YellowBlue','BlackYellow'], + ['GreenBlack','BlackYellow','Black'] +); + +foreach my $ring (@rings_components) { + my $internal_sum = 11; + foreach my $c (@$ring){ + $values{$c} = $internal_sum unless defined $values{$c}; + $internal_sum -= $values{$c}; + } +} + +printf "%s => %d\n",$_,$values{$_} for sort keys %values; + +__END__ + +./ch-1.pl +Black => 6 +BlackYellow => 1 +Blue => 8 +Green => 5 +GreenBlack => 4 +Red => 9 +RedGreen => 2 +Yellow => 7 +YellowBlue => 3 diff --git a/challenge-043/andrezgz/perl/ch-2.pl b/challenge-043/andrezgz/perl/ch-2.pl new file mode 100644 index 0000000000..9c986f5bd6 --- /dev/null +++ b/challenge-043/andrezgz/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/perl + +# https://perlweeklychallenge.org/blog/perl-weekly-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. +# https://en.wikipedia.org/wiki/Self-descriptive_number + +use strict; +use warnings; + +my $base = shift || 10; +die "No self-descriptive numbers in base $base" if ($base <4 || $base == 6); + +if ($base >= 7) { + my @symbols = (0..9,'A'..'Z'); + print $symbols[$base - 4] . '21' . '0' x ($base - 7) . '1000' . $/; +} +else{ + my $from = '1' . '0' x ($base-1); + my $to = '9' x $base; + + for my $n ($from .. $to) { + my @count = (0) x $base; + $count[$_]++ for split //, $n; + print $n.$/ if ($n eq join '',@count[0..$base-1]); + } +} + +__END__ + +./ch-2.pl 4 +1210 +2020 + +./ch-2.pl 6 +No self-descriptive numbers in base 6 + +./ch-2.pl 10 +6210001000 + +./ch-2.pl 16 +C210000000001000 |
