diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-02 12:59:28 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-02 12:59:28 +0100 |
| commit | 4b1851dda8bcec1c9f4adb5fb9eda12e071baedc (patch) | |
| tree | 57b9639e813515f1211e7203858a3dea27f9f0d5 | |
| parent | 0512711fccf91c731495f1dd901349cc900ec299 (diff) | |
| parent | db1207e256584adcd397918a9c327aedb3b4c941 (diff) | |
| download | perlweeklychallenge-club-4b1851dda8bcec1c9f4adb5fb9eda12e071baedc.tar.gz perlweeklychallenge-club-4b1851dda8bcec1c9f4adb5fb9eda12e071baedc.tar.bz2 perlweeklychallenge-club-4b1851dda8bcec1c9f4adb5fb9eda12e071baedc.zip | |
Merge pull request #10752 from pme/challenge-207
challenge-207
| -rwxr-xr-x | challenge-207/peter-meszaros/perl/ch-1.pl | 69 | ||||
| -rwxr-xr-x | challenge-207/peter-meszaros/perl/ch-2.pl | 66 |
2 files changed, 135 insertions, 0 deletions
diff --git a/challenge-207/peter-meszaros/perl/ch-1.pl b/challenge-207/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..090e7d39f4 --- /dev/null +++ b/challenge-207/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl +# +=head1 Task 1: Keyboard Word + +Submitted by: Mohammad S Anwar + +You are given an array of words. + +Write a script to print all the words in the given array that can be types +using alphabet on only one row of the keyboard. + +Let us assume the keys are arranged as below: + + Row 1: qwertyuiop + Row 2: asdfghjkl + Row 3: zxcvbnm + +=head2 Example 1 + + Input: @words = ("Hello","Alaska","Dad","Peace") + Output: ("Alaska","Dad") + +=head2 Example 2 + + Input: @array = ("OMG","Bye") + Output: () + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use List::Util qw/all/; + +my $cases = [ + [["Hello","Alaska","Dad","Peace"], ["Alaska","Dad"], 'Example 1'], + [["OMG","Bye"], [], 'Example 2'], +]; + +my $kbds = [ + {map {$_ => 1} qw/q w e r t y u i o p/}, + {map {$_ => 1} qw/a s d f g h j k l/}, + {map {$_ => 1} qw/z x c v b n m/}, +]; + +sub keyboard_word +{ + my $l = shift; + + my @res; + for my $w (@$l) { + my @w = map { lc } split //, $w; + for my $kbd (@$kbds) { + if (all { defined } $kbd->@{@w}) { + push @res, $w; + last; + } + } + } + return \@res; +} + +for (@$cases) { + is(keyboard_word($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-207/peter-meszaros/perl/ch-2.pl b/challenge-207/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..62f289ee02 --- /dev/null +++ b/challenge-207/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/env perl +# +=head1 Task 2: H-Index + +Submitted by: Mohammad S Anwar + +You are given an array of integers containing citations a researcher has +received for each paper. + +Write a script to compute the researcher's H-Index. For more information please +checkout the wikipedia page. + + The H-Index is the largest number h such that h articles have at least h + citations each. For example, if an author has five publications, with 9, 7, + 6, 2, and 1 citations (ordered from greatest to least), then the author's + h-index is 3, because the author has three publications with 3 or more + citations. However, the author does not have four publications with 4 or + more citations. + +=head2 Example 1 + + Input: @citations = (10,8,5,4,3) + Output: 4 + + Because the 4th publication has 4 citations and the 5th has only 3. + +=head2 Example 2 + + Input: @citations = (25,8,5,3,3) + Output: 3 + + The H-Index is 3 because the fourth paper has only 3 citations. + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[10, 8, 5, 4, 3], 4, 'Example 1'], + [[25, 8, 5, 3, 3], 3, 'Example 2'], +]; + +sub h_index +{ + my $l = shift; + + my $h = 0; + + for my $i (0 .. $#$l) { + if ($l->[$i] < $i) { + $h = $l->[$i-1]; + last; + } + } + return $h; +} + +for (@$cases) { + is(h_index($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; |
