aboutsummaryrefslogtreecommitdiff
path: root/challenge-105
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-03-24 05:09:37 +0000
committerGitHub <noreply@github.com>2021-03-24 05:09:37 +0000
commit37c241fd844312e0fac4509ad94d7925af2b83bb (patch)
tree5e214de6a72c69ad46a6a9a14b0f05d4b5b63888 /challenge-105
parenta04b378d274f17fcb9444de40dc554349e3a05b2 (diff)
parent871d48775c79fafcd90a2ce7eb1b3f7933ea2cfc (diff)
downloadperlweeklychallenge-club-37c241fd844312e0fac4509ad94d7925af2b83bb.tar.gz
perlweeklychallenge-club-37c241fd844312e0fac4509ad94d7925af2b83bb.tar.bz2
perlweeklychallenge-club-37c241fd844312e0fac4509ad94d7925af2b83bb.zip
Merge pull request #3765 from choroba/ech105
Add solutions to 105: Nth root & The Name Game by E. Choroba
Diffstat (limited to 'challenge-105')
-rwxr-xr-xchallenge-105/e-choroba/perl/ch-1.pl22
-rwxr-xr-xchallenge-105/e-choroba/perl/ch-2.pl45
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';