diff options
| -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'; |
