diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-03-08 19:54:28 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-08 19:54:28 +0000 |
| commit | 29ed6fe487eded52298d199ece043e69119fbf02 (patch) | |
| tree | 60cc46e348dbe6a809e63f60df75fdc297b2921d | |
| parent | 6a725f22e954465a598408c4f42995818d8b94ea (diff) | |
| parent | 4dddd9deec2a56bd8ff9372413fdf84240aa191c (diff) | |
| download | perlweeklychallenge-club-29ed6fe487eded52298d199ece043e69119fbf02.tar.gz perlweeklychallenge-club-29ed6fe487eded52298d199ece043e69119fbf02.tar.bz2 perlweeklychallenge-club-29ed6fe487eded52298d199ece043e69119fbf02.zip | |
Merge pull request #7684 from jacoby/master
#207 DAJ
| -rw-r--r-- | challenge-207/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-207/dave-jacoby/perl/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-207/dave-jacoby/perl/ch-2.pl | 31 |
3 files changed, 74 insertions, 0 deletions
diff --git a/challenge-207/dave-jacoby/blog.txt b/challenge-207/dave-jacoby/blog.txt new file mode 100644 index 0000000000..5ad094dd9f --- /dev/null +++ b/challenge-207/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2023/03/06/multistatus-weekly-challenge-207.html diff --git a/challenge-207/dave-jacoby/perl/ch-1.pl b/challenge-207/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..acfd7f006c --- /dev/null +++ b/challenge-207/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +use DateTime; + +my @examples = ( + + [qw{ Hello Alaska Dad Peace }], + [qw{ OMG Bye }], +); + +for my $e (@examples) { + my $list = join ',', map { qq{"$_"} } $e->@*; + my $out = keyboard_word( $e->@* ); + my $olist = join ',', map { qq{"$_"} } $out->@*; + say <<"END"; + Input: \@array = ($list) + Output: ($olist) +END +} + +sub keyboard_word ( @array ) { + my @output; + my %row_1 = map { $_ => 1 } split //, 'qwertyuiop'; + my %row_2 = map { $_ => 1 } split //, 'asdfghjkl'; + my %row_3 = map { $_ => 1 } split //, 'zxcvbnm'; + + for my $Word (@array) { + my $count; + for my $l ( map { fc $_ } split //, $Word ) { + $count->{1}++ if $row_1{$l}; + $count->{2}++ if $row_2{$l}; + $count->{3}++ if $row_3{$l}; + } + push @output, $Word unless scalar keys $count->%* > 1; + } + return wantarray ? @output : \@output; +} + diff --git a/challenge-207/dave-jacoby/perl/ch-2.pl b/challenge-207/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..3d7af432e1 --- /dev/null +++ b/challenge-207/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ max }; + +my @examples = ( + + [ 10, 8, 5, 4, 3 ], + [ 25, 8, 5, 3, 3 ], +); + +for my $e (@examples) { + my $o = h_index( $e->@* ); + my $array = join ', ', $e->@*; + say <<"END"; + Input: \@array = $array + Output: $o +END +} + +sub h_index ( @citations ) { + my $max = max @citations; + for my $h ( 1 .. $max ) { + my $i = () = grep { $_ >= $h } @citations; + return $h - 1 if $i < $h; + } + return 0; +} |
