diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-22 11:32:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-22 11:32:40 +0100 |
| commit | 774e413698f514d0281e850df623bc9906d127f5 (patch) | |
| tree | 902b1c9cbc22b3aaa46fbdebd2f1c1881f93c894 | |
| parent | a01b4632d06c441f117d9c6a1c4794384f665c0f (diff) | |
| parent | 8a267312f3c5c02f70e31ca1a5dc3afabd9a82d1 (diff) | |
| download | perlweeklychallenge-club-774e413698f514d0281e850df623bc9906d127f5.tar.gz perlweeklychallenge-club-774e413698f514d0281e850df623bc9906d127f5.tar.bz2 perlweeklychallenge-club-774e413698f514d0281e850df623bc9906d127f5.zip | |
Merge pull request #10475 from PerlBoy1967/branch-for-challenge-279
w278 & w279, task 1 & 2
| -rwxr-xr-x | challenge-278/perlboy1967/perl/ch1.pl | 38 | ||||
| -rwxr-xr-x | challenge-278/perlboy1967/perl/ch2.pl | 35 | ||||
| -rwxr-xr-x | challenge-279/perlboy1967/perl/ch1.pl | 34 | ||||
| -rwxr-xr-x | challenge-279/perlboy1967/perl/ch2.pl | 35 |
4 files changed, 142 insertions, 0 deletions
diff --git a/challenge-278/perlboy1967/perl/ch1.pl b/challenge-278/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..fe27751f6b --- /dev/null +++ b/challenge-278/perlboy1967/perl/ch1.pl @@ -0,0 +1,38 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 278 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-278 + +Author: Niels 'PerlBoy' van Dijke + +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. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0 qw(-no_srand); + +sub sortString ($str) { + join ' ',map {$_->[0]} sort {$$a[1]<=>$$b[1]} map {[/(\D+)(\d+)/]} split /\s+/,$str +} + +is(sortString('and2 Raku3 cousins5 Perl1 are4'), + 'Perl and Raku are cousins'); +is(sortString('guest6 Python1 most4 the3 popular5 is2 language7'), + 'Python is the most popular guest language'); +is(sortString('Challenge3 The1 Weekly2'), + 'The Weekly Challenge'); + +done_testing; diff --git a/challenge-278/perlboy1967/perl/ch2.pl b/challenge-278/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..93f2536312 --- /dev/null +++ b/challenge-278/perlboy1967/perl/ch2.pl @@ -0,0 +1,35 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 278 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-278 + +Author: Niels 'PerlBoy' van Dijke + +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. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0 qw(-no_srand); + +sub reverseWord ($word,$char) { + $word =~ s#^(.*?$char)#join('',sort split //,$1)#er; +} + +is(reverseWord('challenge','e'),'acehllnge'); +is(reverseWord('programming','a'),'agoprrmming'); +is(reverseWord('champion','b'),'champion'); + +done_testing; diff --git a/challenge-279/perlboy1967/perl/ch1.pl b/challenge-279/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..e9daf84f13 --- /dev/null +++ b/challenge-279/perlboy1967/perl/ch1.pl @@ -0,0 +1,34 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 279 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-279 + +Author: Niels 'PerlBoy' van Dijke + +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. + +=cut + +use v5.32; +use feature qw(signatures); +use common::sense; + +use Test2::V0 qw(-no_srand); + +sub sortLetters :prototype(\@\@) ($arL,$arW) { + my $i = 0; + join '', map {$$_[0]} sort { $$a[1]<=>$$b[1] } map {[$_,$$arW[$i++]]} @$arL; +} + +is(sortLetters(@{[qw{R E P L}]},@{[qw{3 2 1 4}]}),'PERL','Example 1'); +is(sortLetters(@{[qw{A U R K}]},@{[qw{2 4 1 3}]}),'RAKU','Example 2'); +is(sortLetters(@{[qw{O H Y N P T}]},@{[qw{5 4 2 6 1 3}]}),'PYTHON','Example 3'); + +done_testing; diff --git a/challenge-279/perlboy1967/perl/ch2.pl b/challenge-279/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..19baec3feb --- /dev/null +++ b/challenge-279/perlboy1967/perl/ch2.pl @@ -0,0 +1,35 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 279 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-279 + +Author: Niels 'PerlBoy' van Dijke + +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. + +=cut + +use v5.32; +no warnings 'experimental::signatures'; +use feature qw(signatures); +use common::sense; + +use Test2::V0 qw(-no_srand); + +sub splitString ($str) { + $str =~ tr/aeiouy// % 2 ? 0 : $str =~ m#[aeiouy][^aeiouy]+$# ? 1 : 0; +} + +is(splitString('perl'),0,'Example 1'); +is(splitString('book'),1,'Example 2'); +is(splitString('good morning'),1,'Example 2'); + +done_testing; |
