From 2bbf531aeb7f75db241aba108b1eda2ebb54f7eb Mon Sep 17 00:00:00 2001 From: KUEPPO Date: Mon, 7 Nov 2022 01:20:46 +0100 Subject: ch-1.pl pwc-189 --- challenge-189/kueppo-wesley/Perl/ch-1.pl | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 challenge-189/kueppo-wesley/Perl/ch-1.pl diff --git a/challenge-189/kueppo-wesley/Perl/ch-1.pl b/challenge-189/kueppo-wesley/Perl/ch-1.pl new file mode 100644 index 0000000000..367b27cc00 --- /dev/null +++ b/challenge-189/kueppo-wesley/Perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use Test::More; +use List::Util qw/ first /; + +sub find_greater_char { + my ( $array, $target ) = @_; + + return ( first { $_ gt $target } sort { $a cmp $b } @$array ) // $target; +} + +my $expected = [ qw/e c r c v/ ]; +my @samples = ( + [ [ qw/e m u g/ ], q/b/ ], + [ [ qw/d c e f/ ], q/a/ ], + [ [ qw/j a r/ ], q/o/ ], + [ [ qw/d c a f/ ], q/a/ ], + [ [ qw/t g a l/ ], q/v/ ] +); + +is_deeply( $expected, [ map { find_greater_char( @$_ ) } @samples ], 'found the correct chars?' ); + +done_testing( 1 ); -- cgit From 58e4bdc5559742b59d1d6270800e61ae97bb2c4c Mon Sep 17 00:00:00 2001 From: KUEPPO Date: Mon, 7 Nov 2022 01:28:13 +0100 Subject: ch-2.pl pwc-189 --- challenge-189/kueppo-wesley/Perl/ch-2.pl | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 challenge-189/kueppo-wesley/Perl/ch-2.pl diff --git a/challenge-189/kueppo-wesley/Perl/ch-2.pl b/challenge-189/kueppo-wesley/Perl/ch-2.pl new file mode 100644 index 0000000000..53caa3f16d --- /dev/null +++ b/challenge-189/kueppo-wesley/Perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw/for_list/; +use builtin qw/indexed/; + +no warnings 'experimental::builtin'; + +use Test::More; +use Test::Deep; + +sub find_min_slice { + my $freq; + my $target = ''; + + $freq->{$target}->[ 0 ] = 0; + foreach my ( $index, $int ) ( indexed @_ ) { + $freq->{$int}->[ 0 ]++; + $freq->{$int}->[ 1 ] = $index unless defined $freq->{$int}->[ 1 ]; + $freq->{$int}->[ 2 ] = $index; + + $freq->{$int}->[ 3 ] = 1 + $index - $freq->{$int}->[ 1 ]; + if ( ( $freq->{$int}->[ 3 ] < ( $freq->{$target}->[ 3 ] // -1 ) && $freq->{$int}->[ 0 ] == $freq->{$target}->[ 0 ] ) + || $freq->{$int}->[ 0 ] > $freq->{$target}->[ 0 ] ) { + $target = $int; + } + } + + return [ @_[ $freq->{$target}->[ 1 ] .. $freq->{$target}->[ 2 ] ] ]; +} + +my @samples = ( + [ qw/1 3 3 2/ ], + [ qw/1 2 1 3/ ], + [ qw/1 3 2 1 2/ ], + [ qw/1 1 2 3 2/ ], + [ qw/2 1 2 1 1/ ], +); + +my $expected = [ + [ qw/3 3/ ], + [ qw/1 2 1/ ], + [ qw/2 1 2/ ], + [ qw/1 1/ ], + [ qw/1 2 1 1/ ], +]; + +cmp_deeply( [ map { find_min_slice( @$_ ) } @samples ], $expected, "Were correctly sliced?" ); +done_testing( 1 ); -- cgit From 6e598a1fa67fa07e50b6b4979ac3f76800f40bca Mon Sep 17 00:00:00 2001 From: KUEPPO Date: Mon, 7 Nov 2022 01:29:26 +0100 Subject: ch-1.pl pwc-189 --- challenge-189/kueppo-wesley/Perl/ch-1.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-189/kueppo-wesley/Perl/ch-1.pl b/challenge-189/kueppo-wesley/Perl/ch-1.pl index 367b27cc00..3b61a3861c 100644 --- a/challenge-189/kueppo-wesley/Perl/ch-1.pl +++ b/challenge-189/kueppo-wesley/Perl/ch-1.pl @@ -4,7 +4,7 @@ use strict; use warnings; use Test::More; -use List::Util qw/ first /; +use List::Util qw/first/; sub find_greater_char { my ( $array, $target ) = @_; @@ -21,6 +21,6 @@ my @samples = ( [ [ qw/t g a l/ ], q/v/ ] ); -is_deeply( $expected, [ map { find_greater_char( @$_ ) } @samples ], 'found the correct chars?' ); +is_deeply( [ map { find_greater_char( @$_ ) } @samples ], $expected, 'found the correct chars?' ); done_testing( 1 ); -- cgit