diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-16 11:42:43 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-16 11:42:43 +0100 |
| commit | 6d0af37dbeba795de3a812874f1d711995d16c2d (patch) | |
| tree | 7f08662ff4d67a4f014fc9e0d7b9dc2090719512 | |
| parent | 19a4cb8fee3c267bfaace7516b8bde355add095f (diff) | |
| parent | 78a298e4b222a07c9ed8c7ec99a6b66efb6c6949 (diff) | |
| download | perlweeklychallenge-club-6d0af37dbeba795de3a812874f1d711995d16c2d.tar.gz perlweeklychallenge-club-6d0af37dbeba795de3a812874f1d711995d16c2d.tar.bz2 perlweeklychallenge-club-6d0af37dbeba795de3a812874f1d711995d16c2d.zip | |
Merge pull request #10434 from choroba/ech278
Add solutions to 278: Sort String & Reverse Word by E. Choroba
| -rwxr-xr-x | challenge-278/e-choroba/perl/ch-1.pl | 29 | ||||
| -rwxr-xr-x | challenge-278/e-choroba/perl/ch-2.pl | 21 |
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'; |
