diff options
| author | pme <hauptadler@gmail.com> | 2024-07-15 16:34:38 +0200 |
|---|---|---|
| committer | pme <hauptadler@gmail.com> | 2024-07-15 16:34:38 +0200 |
| commit | 88cb4c5af54ff41d3c66e6645ed1739256db37a1 (patch) | |
| tree | 6ce27f956f3b31c1788cb61af170b9aaea9432a2 | |
| parent | f1533357698083086127e85e17fd8e2a80780e76 (diff) | |
| download | perlweeklychallenge-club-88cb4c5af54ff41d3c66e6645ed1739256db37a1.tar.gz perlweeklychallenge-club-88cb4c5af54ff41d3c66e6645ed1739256db37a1.tar.bz2 perlweeklychallenge-club-88cb4c5af54ff41d3c66e6645ed1739256db37a1.zip | |
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; + |
