diff options
| -rwxr-xr-x | challenge-105/e-choroba/perl/ch-1.pl | 22 | ||||
| -rwxr-xr-x | challenge-105/e-choroba/perl/ch-2.pl | 45 |
2 files changed, 67 insertions, 0 deletions
diff --git a/challenge-105/e-choroba/perl/ch-1.pl b/challenge-105/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..3c213e9df5 --- /dev/null +++ b/challenge-105/e-choroba/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use warnings; +use strict; + +=head1 Nth root + +The real challenge here was to get the formatting of the result right. +34 ** (1/5) is in fact 2.02439745849989, which we can format with +sprintf '%.2f', but that would turn 12 in the first test into 12.00. +Adding it to zero fixes the issue. + +=cut + +sub nth_root { + my ($n, $k) = @_; + return 0 + sprintf '%.2f', $k ** (1 / $n) +} + +use Test::More tests => 2; + +is nth_root(5, 248832), 12; +is nth_root(5, 34), 2.02; diff --git a/challenge-105/e-choroba/perl/ch-2.pl b/challenge-105/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..4353c74e60 --- /dev/null +++ b/challenge-105/e-choroba/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl +use warnings; +use strict; + +sub name_game { + my ($name) = @_; + + my %name; + if ($name =~ /^[^AEIOU]/) { + %name = map { + $_ => ($_ x (0 != index $name, uc)) . substr $name, 1 + } qw( b f m ); + + } else { + $name{$_} = $_ . lc $name for qw( b f m ); + } + + return sprintf join("\n", '%s, %s, bo-%s,', + 'Bonana-fanna fo-%s', + 'Fee fi mo-%s', + '%s!'), + ($name) x 2, @name{qw{ b f m }}, $name +} + +use Test::More tests => 4; + +is name_game('Katie'), + "Katie, Katie, bo-batie,\nBonana-fanna fo-fatie\nFee fi mo-matie\nKatie!", + 'Katie'; + +is name_game('Billy'), + "Billy, Billy, bo-illy,\nBonana-fanna fo-filly\nFee fi mo-milly\nBilly!", + 'Billy'; + +is name_game('Fred'), + "Fred, Fred, bo-bred,\nBonana-fanna fo-red\nFee fi mo-mred\nFred!", + 'Fred'; + +is name_game('Marsha'), + "Marsha, Marsha, bo-barsha,\nBonana-fanna fo-farsha\nFee fi mo-arsha\nMarsha!", + 'Marsha'; + +is name_game('Anna'), + "Anna, Anna, bo-banna,\nBonana-fanna fo-fanna\nFee fi mo-manna\nAnna!", + 'Anna'; |
