aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2024-07-15 11:45:00 +0200
committerE. Choroba <choroba@matfyz.cz>2024-07-15 11:45:00 +0200
commit78a298e4b222a07c9ed8c7ec99a6b66efb6c6949 (patch)
treede7a70babccb17b06d4a9cd000db1a8b9022313a
parentf1533357698083086127e85e17fd8e2a80780e76 (diff)
downloadperlweeklychallenge-club-78a298e4b222a07c9ed8c7ec99a6b66efb6c6949.tar.gz
perlweeklychallenge-club-78a298e4b222a07c9ed8c7ec99a6b66efb6c6949.tar.bz2
perlweeklychallenge-club-78a298e4b222a07c9ed8c7ec99a6b66efb6c6949.zip
Add solutions to 278: Sort String & Reverse Word by E. Choroba
-rwxr-xr-xchallenge-278/e-choroba/perl/ch-1.pl29
-rwxr-xr-xchallenge-278/e-choroba/perl/ch-2.pl21
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-278/e-choroba/perl/ch-1.pl b/challenge-278/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..87ab5ee989
--- /dev/null
+++ b/challenge-278/e-choroba/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub sort_string($str) {
+ my @words;
+ $words[ $2 - 1 ] = $1 while $str =~ /(\S+?)([0-9]+)/g;
+ return "@words"
+}
+
+use Test::More tests => 3 + 1;
+
+is sort_string('and2 Raku3 cousins5 Perl1 are4'),
+ 'Perl and Raku are cousins',
+ 'Example 1';
+
+is sort_string('guest6 Python1 most4 the3 popular5 is2 language7'),
+ 'Python is the most popular guest language',
+ 'Example 2';
+
+is sort_string('Challenge3 The1 Weekly2'),
+ 'The Weekly Challenge',
+ 'Example 3';
+
+is sort_string(
+ 'eleven11 ten10 nine9 eight8 seven7 six6 five5 four4 three3 two2 one1'),
+ 'one two three four five six seven eight nine ten eleven',
+ 'multiple digits';
diff --git a/challenge-278/e-choroba/perl/ch-2.pl b/challenge-278/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..b450858c54
--- /dev/null
+++ b/challenge-278/e-choroba/perl/ch-2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub reverse_word($word, $char) {
+ if ((my $index = 1 + index $word, $char) > 0) {
+ substr $word, 0, $index,
+ join "", sort split //, substr $word, 0, $index;
+ }
+ return $word
+}
+
+use Test::More tests => 3 + 2;
+
+is reverse_word('challenge', 'e'), 'acehllnge', 'Example 1';
+is reverse_word('programming', 'a'), 'agoprrmming', 'Example 2';
+is reverse_word('champion', 'b'), 'champion', 'Example 3';
+
+is reverse_word('tome', 't'), 'tome', 'first char';
+is reverse_word('tome', 'e'), 'emot', 'last char';