aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2023-03-06 23:36:07 +0100
committerE. Choroba <choroba@matfyz.cz>2023-03-06 23:36:07 +0100
commit9efae9f67fef43a2f051653597012f51c11c73b6 (patch)
tree5c2ba0ba8c941ef8ca20b97927704557dc5e67f6
parentd75c83429332efc88f29eee14f988b199f2fa10c (diff)
downloadperlweeklychallenge-club-9efae9f67fef43a2f051653597012f51c11c73b6.tar.gz
perlweeklychallenge-club-9efae9f67fef43a2f051653597012f51c11c73b6.tar.bz2
perlweeklychallenge-club-9efae9f67fef43a2f051653597012f51c11c73b6.zip
Add solutions to 207: Keyboard Word & H-Index by E. Choroba
-rwxr-xr-xchallenge-207/e-choroba/perl/ch-1.pl24
-rwxr-xr-xchallenge-207/e-choroba/perl/ch-2.pl21
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';