From 4dddd9deec2a56bd8ff9372413fdf84240aa191c Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 6 Mar 2023 18:01:08 -0500 Subject: #207 DAJ --- challenge-207/dave-jacoby/blog.txt | 1 + challenge-207/dave-jacoby/perl/ch-1.pl | 42 ++++++++++++++++++++++++++++++++++ challenge-207/dave-jacoby/perl/ch-2.pl | 31 +++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 challenge-207/dave-jacoby/blog.txt create mode 100644 challenge-207/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-207/dave-jacoby/perl/ch-2.pl 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; +} -- cgit