diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-22 09:32:13 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-22 09:32:13 +0100 |
| commit | 8d126c1681dba4412dbc1d5d29574bbc375bea15 (patch) | |
| tree | b61ddfc91f9842a0d15f75361c90d4388a279bf9 | |
| parent | f9135963d2e34ff7a4bb0ba2384f94eaa2fc1473 (diff) | |
| parent | d01809d766eb5a42f763e018953f24a48334878c (diff) | |
| download | perlweeklychallenge-club-8d126c1681dba4412dbc1d5d29574bbc375bea15.tar.gz perlweeklychallenge-club-8d126c1681dba4412dbc1d5d29574bbc375bea15.tar.bz2 perlweeklychallenge-club-8d126c1681dba4412dbc1d5d29574bbc375bea15.zip | |
Merge pull request #10470 from pme/challenge-279
challenge-279
| -rwxr-xr-x | challenge-279/peter-meszaros/perl/ch-1.pl | 55 | ||||
| -rwxr-xr-x | challenge-279/peter-meszaros/perl/ch-2.pl | 69 |
2 files changed, 124 insertions, 0 deletions
diff --git a/challenge-279/peter-meszaros/perl/ch-1.pl b/challenge-279/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..d92a44d830 --- /dev/null +++ b/challenge-279/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Sort Letters + +Submitted by: Mohammad Sajid Anwar + +You are given two arrays, @letters and @weights. + +Write a script to sort the given array @letters based on the @weights. + +=head2 Example 1 + + Input: @letters = ('R', 'E', 'P', 'L') + @weights = (3, 2, 1, 4) + Output: PERL + +=head2 Example 2 + + Input: @letters = ('A', 'U', 'R', 'K') + @weights = (2, 4, 1, 3) + Output: RAKU + +=head2 Example 3 + + Input: @letters = ('O', 'H', 'Y', 'N', 'P', 'T') + @weights = (5, 4, 2, 6, 1, 3) + Output: PYTHON + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[['R','E','P','L'], [3, 2, 1, 4]], 'PERL'], + [[['A','U','R','K'], [2, 4, 1, 3]], 'RAKU'], + [[['O','H','Y','N','P','T'], [5, 4, 2, 6, 1, 3]], 'PYTHON'], +]; + +sub sort_letters +{ + my $l = $_[0]->[0]; + my $w = $_[0]->[1]; + + return join '', $l->@[sort { $w->[$a] cmp $w->[$b] } 0 .. $#$l]; +} + +for (@$cases) { + is(sort_letters($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-279/peter-meszaros/perl/ch-2.pl b/challenge-279/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..acb2efeadc --- /dev/null +++ b/challenge-279/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl +# +=head1 Task 2: Split String + +Submitted by: Mohammad Sajid Anwar + +You are given a string, $str. + +Write a script to split the given string into two containing exactly same +number of vowels and return true if you can otherwise false. + +=head2 Example 1 + + Input: $str = "perl" + Ouput: false + +=head2 Example 2 + + Input: $str = "book" + Ouput: true + +Two possible strings "bo" and "ok" containing exactly one vowel each. + +=head2 Example 3 + + Input: $str = "good morning" + Ouput: true + +Two possible strings "good " and "morning" containing two vowels each or +"good m" and "orning" containing two vowels each. + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +use constant true => 1; +use constant false => 0; + +my $cases = [ + ["perl", false], + ["book", true], + ["good morning", true], +]; + +my $wovels = { + a => 1, + e => 1, + i => 1, + o => 1, + u => 1, +}; + +sub split_string +{ + my $str = shift; + + return ((grep {$wovels->{$_}} split '', $str) % 2) ? false : true; +} + +for (@$cases) { + is(split_string($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; + |
