diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-08 19:52:45 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-08 19:52:45 +0000 |
| commit | 6a725f22e954465a598408c4f42995818d8b94ea (patch) | |
| tree | ccae378913d96ecbad9c0a3d1cb5fdb0dce79a96 | |
| parent | 3a6ff4e60e9c97c2644ce9463bb522077c36a2bc (diff) | |
| parent | 9efae9f67fef43a2f051653597012f51c11c73b6 (diff) | |
| download | perlweeklychallenge-club-6a725f22e954465a598408c4f42995818d8b94ea.tar.gz perlweeklychallenge-club-6a725f22e954465a598408c4f42995818d8b94ea.tar.bz2 perlweeklychallenge-club-6a725f22e954465a598408c4f42995818d8b94ea.zip | |
Merge pull request #7683 from choroba/ech207
Add solutions to 207: Keyboard Word & H-Index by E. Choroba
| -rwxr-xr-x | challenge-207/e-choroba/perl/ch-1.pl | 24 | ||||
| -rwxr-xr-x | challenge-207/e-choroba/perl/ch-2.pl | 21 |
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-207/e-choroba/perl/ch-1.pl b/challenge-207/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..51a1cc6175 --- /dev/null +++ b/challenge-207/e-choroba/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +my $i = 0; +my %ROW = map { ++$i; map { $_ => $i } split // } + qw( qwertyuiop asdfghjkl zxcvbnm ); + +sub keyboard_word(@words) { + return [grep is_single_row($_), @words] +} + +sub is_single_row($word) { + my %rows; + @rows{ @ROW{ split //, lc $word } } = (); + return 1 == keys %rows +} + +use Test2::V0; +plan 2; + +is keyboard_word(qw( Hello Alaska Dad Peace )), [qw[ Alaska Dad ]], 'Example 1'; +is keyboard_word(qw( OMG Bye )), [], 'Example 2'; diff --git a/challenge-207/e-choroba/perl/ch-2.pl b/challenge-207/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..09343f121f --- /dev/null +++ b/challenge-207/e-choroba/perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +sub h_index(@citations) { + @citations = sort { $b <=> $a } @citations; + push @citations, -1; + for my $i (0 .. $#citations) { + return $i if $citations[$i] < $i + 1; + } +} + +use Test::More tests => 2 + 3; + +is h_index(10, 8, 5, 4, 3), 4, 'Example 1'; +is h_index(25, 8, 5, 3, 3), 3, 'Example 2'; + +is h_index(9, 7, 6, 2, 1), 3, 'Wikipedia'; +is h_index(100), 1, 'Single publication'; +is h_index(1, 1, 1, 1), 1, 'All ones'; |
