diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-16 11:44:18 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-16 11:44:18 +0100 |
| commit | 05543cdb3954eee696d07b5541fdd9eb15fd1c66 (patch) | |
| tree | 9c88d0827a9fb591e006f77f2f943ce1288c5a9d | |
| parent | 1f04d6355e6be40160565d5a29957c61c8a37e26 (diff) | |
| parent | 88cb4c5af54ff41d3c66e6645ed1739256db37a1 (diff) | |
| download | perlweeklychallenge-club-05543cdb3954eee696d07b5541fdd9eb15fd1c66.tar.gz perlweeklychallenge-club-05543cdb3954eee696d07b5541fdd9eb15fd1c66.tar.bz2 perlweeklychallenge-club-05543cdb3954eee696d07b5541fdd9eb15fd1c66.zip | |
Merge pull request #10437 from pme/challenge-278
challenge-278
| -rwxr-xr-x | challenge-278/peter-meszaros/perl/ch-1.pl | 58 | ||||
| -rwxr-xr-x | challenge-278/peter-meszaros/perl/ch-2.pl | 56 |
2 files changed, 114 insertions, 0 deletions
diff --git a/challenge-278/peter-meszaros/perl/ch-1.pl b/challenge-278/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..a7eeeaaf6b --- /dev/null +++ b/challenge-278/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Sort String + +Submitted by: Mohammad Sajid Anwar + +You are given a shuffle string, $str. + +Write a script to return the sorted string. + + A string is shuffled by appending word position to each word. + +=head2 Example 1 + + Input: $str = "and2 Raku3 cousins5 Perl1 are4" + Output: "Perl and Raku are cousins" + +=head2 Example 2 + + Input: $str = "guest6 Python1 most4 the3 popular5 is2 language7" + Output: "Python is the most popular guest language" + +=head2 Example 3 + + Input: $str = "Challenge3 The1 Weekly2" + Output: "The Weekly Challenge" + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + ["and2 Raku3 cousins5 Perl1 are4", + "Perl and Raku are cousins"], + ["guest6 Python1 most4 the3 popular5 is2 language7", + "Python is the most popular guest language"], + ["Challenge3 The1 Weekly2", + "The Weekly Challenge"], +]; + +sub sort_string +{ + my $str = shift; + + my @str = split(/ /, $str); + @str = sort { substr($a, -1, 1) <=> substr($b, -1, 1) } @str; + return join(' ', map { substr($_, 0, -1) } @str); +} + +for (@$cases) { + is(sort_string($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-278/peter-meszaros/perl/ch-2.pl b/challenge-278/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..ad53017f91 --- /dev/null +++ b/challenge-278/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Reverse Word + +Submitted by: Mohammad Sajid Anwar + +You are given a word, $word and a character, $char. + +Write a script to replace the substring up to and including $char with its +characters sorted alphabetically. If the $char doesn't exist then DON'T do +anything. + +=head2 Example 1 + + Input: $str = "challenge", $char = "e" + Ouput: "acehllnge" + +=head2 Example 2 + + Input: $str = "programming", $char = "a" + Ouput: "agoprrmming" + +=head2 Example 3 + + Input: $str = "champion", $char = "b" + Ouput: "champion" + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[ "challenge", "e"], "acehllnge"], + [[ "programming", "a"], "agoprrmming"], + [[ "champion", "b"], "champion"], +]; + +sub reverse_word +{ + my $w = $_[0]->[0]; + my $c = $_[0]->[1]; + + $w =~ s/^([a-z]+?${c})/join '', sort split '', $1/e; + return $w; +} + +for (@$cases) { + is(reverse_word($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; + |
