From 78a298e4b222a07c9ed8c7ec99a6b66efb6c6949 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 15 Jul 2024 11:45:00 +0200 Subject: Add solutions to 278: Sort String & Reverse Word by E. Choroba --- challenge-278/e-choroba/perl/ch-1.pl | 29 +++++++++++++++++++++++++++++ challenge-278/e-choroba/perl/ch-2.pl | 21 +++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100755 challenge-278/e-choroba/perl/ch-1.pl create mode 100755 challenge-278/e-choroba/perl/ch-2.pl 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'; -- cgit