aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetilS <kjetilskotheim@gmail.com>2024-07-23 13:08:47 +0200
committerKjetilS <kjetilskotheim@gmail.com>2024-07-23 13:08:47 +0200
commitea2290e25fa0edb47ee4cfc1155a3a13e0639786 (patch)
tree86600116b407afb45fc605098ee3831cbacbc006
parent7980d6f131eaca30585a7f2e5fdadec8ed5812fa (diff)
downloadperlweeklychallenge-club-ea2290e25fa0edb47ee4cfc1155a3a13e0639786.tar.gz
perlweeklychallenge-club-ea2290e25fa0edb47ee4cfc1155a3a13e0639786.tar.bz2
perlweeklychallenge-club-ea2290e25fa0edb47ee4cfc1155a3a13e0639786.zip
https://theweeklychallenge.org/blog/perl-weekly-challenge-279/
-rw-r--r--challenge-274/kjetillll/perl/ch-1.pl30
-rw-r--r--challenge-274/kjetillll/perl/ch-2.pl0
-rw-r--r--challenge-279/kjetillll/perl/ch-1.pl25
-rw-r--r--challenge-279/kjetillll/perl/ch-2.pl8
4 files changed, 63 insertions, 0 deletions
diff --git a/challenge-274/kjetillll/perl/ch-1.pl b/challenge-274/kjetillll/perl/ch-1.pl
new file mode 100644
index 0000000000..2de85ac6f4
--- /dev/null
+++ b/challenge-274/kjetillll/perl/ch-1.pl
@@ -0,0 +1,30 @@
+use strict; use warnings;
+
+sub f {
+ my $end = '';
+ shift()
+ =~ s{ (\S) (\S*) }
+ {
+ my( $first, $rest ) = ($1,$2);
+ $end .= 'a';
+ $first =~ /[aeiou]/i
+ ? $first . $rest . 'ma' . $end
+ : $rest . $first . 'ma' . $end
+ }exgr
+}
+
+
+use Test::More tests => 3;
+is f( $$_{input} ), $$_{output} for
+{
+ input => 'I love Perl',
+ output => 'Imaa ovelmaaa erlPmaaaa'
+},
+{
+ input => 'Perl and Raku are friends',
+ output => 'erlPmaa andmaaa akuRmaaaa aremaaaaa riendsfmaaaaaa'
+},
+{
+ input => 'The Weekly Challenge',
+ output => 'heTmaa eeklyWmaaa hallengeCmaaaa'
+}
diff --git a/challenge-274/kjetillll/perl/ch-2.pl b/challenge-274/kjetillll/perl/ch-2.pl
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/challenge-274/kjetillll/perl/ch-2.pl
diff --git a/challenge-279/kjetillll/perl/ch-1.pl b/challenge-279/kjetillll/perl/ch-1.pl
new file mode 100644
index 0000000000..929ff0b1c7
--- /dev/null
+++ b/challenge-279/kjetillll/perl/ch-1.pl
@@ -0,0 +1,25 @@
+use strict; use warnings; use Test::More tests=>3;
+
+sub sort_letters_by_weight {
+ my @letters = @{ shift() };
+ my @weights = @{ shift() };
+
+ my %w; @w{@letters} = @weights;
+
+ #do the Schwartzian: https://en.wikipedia.org/wiki/Schwartzian_transform
+ return join '',
+ map $$_[0],
+ sort{ $$a[1] <=> $$b[1] || $$a[0] cmp $$b[0] }
+ map [$_, $w{$_}],
+ @letters
+
+
+ ##this return instead is a lot simpler but could be slower for large inputs
+ ##since there would be more hash lookups:
+ #return join '', sort{ $w{$a} <=> $w{$b} || $a cmp $b } @letters
+}
+
+is sort_letters_by_weight( $$_{letters}, $$_{weights} ), $$_{output} for
+ { letters => ['R', 'E', 'P', 'L'], weights => [3, 2, 1, 4], output => 'PERL' },
+ { letters => ['A', 'U', 'R', 'K'], weights => [2, 4, 1, 3], output => 'RAKU' },
+ { letters => ['O', 'H', 'Y', 'N', 'P', 'T'], weights => [5, 4, 2, 6, 1, 3], output => 'PYTHON' }
diff --git a/challenge-279/kjetillll/perl/ch-2.pl b/challenge-279/kjetillll/perl/ch-2.pl
new file mode 100644
index 0000000000..49ec9c63fc
--- /dev/null
+++ b/challenge-279/kjetillll/perl/ch-2.pl
@@ -0,0 +1,8 @@
+use strict; use warnings; use Test::More tests=>3;
+
+sub has_even_vowel_count { 1 - pop =~ y/aeiouAEIOU// % 2 }
+
+ok has_even_vowel_count( $$_{string} ) ? 'true' : 'false' eq $$_{output}
+ for { string => "perl", output => 'false' },
+ { string => "book", output => 'true' },
+ { string => "good morning", output => 'true' }