diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-08 20:12:52 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-08 20:12:52 +0000 |
| commit | 0a2f280b47d88c943bee0bc3b056ba64ee55be9b (patch) | |
| tree | 43647f856da3f6105fdef7e78d52576329e11890 | |
| parent | 5233ca82a0b3784a112dc0c89243666b27650aa1 (diff) | |
| parent | 97dad4d8cbd949bf7db3e0f726f1b730f7ba2735 (diff) | |
| download | perlweeklychallenge-club-0a2f280b47d88c943bee0bc3b056ba64ee55be9b.tar.gz perlweeklychallenge-club-0a2f280b47d88c943bee0bc3b056ba64ee55be9b.tar.bz2 perlweeklychallenge-club-0a2f280b47d88c943bee0bc3b056ba64ee55be9b.zip | |
Merge pull request #7689 from Solathian/branch-for-challenge-207
Adding files for challenge
| -rw-r--r-- | challenge-207/solathian/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-207/solathian/perl/ch-2.pl | 38 |
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-207/solathian/perl/ch-1.pl b/challenge-207/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..2ad2c3cad1 --- /dev/null +++ b/challenge-207/solathian/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!usr/bin/perl +use v5.36; + +# Challenge 207 - 1 -Keyboard Word +# 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 + +my @target = qw(qwertyuiop asdfghjkl zxcvbnm); + +keyboardWord("Hello","Alaska","Dad","Peace"); +keyboardWord("OMG","Bye"); + +sub keyboardWord(@strings) +{ + my @result; + + OUTER:foreach my $string (@strings) + { + INNER: foreach my $row (@target) + { + map{ next INNER if ( index($row, $_) == -1)} split('', lc($string)); + + push(@result, $string); + next OUTER; + } + + } + + say('(', join(', ',@result), ')'); +}
\ No newline at end of file diff --git a/challenge-207/solathian/perl/ch-2.pl b/challenge-207/solathian/perl/ch-2.pl new file mode 100644 index 0000000000..696e384bb5 --- /dev/null +++ b/challenge-207/solathian/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!usr/bin/perl +use v5.36; + +# Challenge 207 - 2 - H index + +# 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. + + +hIndex(9, 7, 6, 2); # 3 +hIndex(10, 8, 5, 4, 3); # 4 +hIndex(25, 8, 5, 3, 3); # 3 +hIndex(1, 1, 1, 1, 1); # 1 +hIndex(2, 2); # 2 + +sub count($threshold, $arrRef) +{ + my $count = 0; + + map{$count++ if($_ >= $threshold)} @$arrRef; + + return $count; +} + +sub hIndex(@publications) +{ + my $h = $publications[0]+1; # consider it already sorted + + do + { + $h-- + } + while( $h > count($h, \@publications)); + + + say $h; +}
\ No newline at end of file |
