diff options
| -rw-r--r-- | challenge-196/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-196/dave-jacoby/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-196/dave-jacoby/perl/ch-2.pl | 42 |
3 files changed, 78 insertions, 0 deletions
diff --git a/challenge-196/dave-jacoby/blog.txt b/challenge-196/dave-jacoby/blog.txt new file mode 100644 index 0000000000..234d3bc22f --- /dev/null +++ b/challenge-196/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2022/12/19/insert-clever-title-here-weekly-challenge-196.html diff --git a/challenge-196/dave-jacoby/perl/ch-1.pl b/challenge-196/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..f0e4b87f0d --- /dev/null +++ b/challenge-196/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +my @examples = + ( [ 3, 1, 4, 2 ], [ 1, 2, 3, 4 ], [ 1, 3, 2, 4, 6, 5 ], [ 1, 3, 4, 2 ], ); + +for my $ex (@examples) { + my @e = $ex->@*; + my $e = join ', ', @e; + my @o = pattern_finder(@e); + my $o = join ', ', @o; + say <<"END"; + Input: \@list = ($e) + Output: ($o) +END +} + +sub pattern_finder ( @array ) { + for my $i ( 0 .. -3 + scalar @array ) { + for my $j ( $i + 1 .. -2 + scalar @array ) { + for my $k ( $j + 1 .. -1 + scalar @array ) { + my $ai = $array[$i]; + my $aj = $array[$j]; + my $ak = $array[$k]; + if ( $ai < $ak && $ak < $aj ) { + return ( $ai, $aj, $ak ); + } + } + } + } + return (); +} diff --git a/challenge-196/dave-jacoby/perl/ch-2.pl b/challenge-196/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..22bf5ae5a5 --- /dev/null +++ b/challenge-196/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ uniq }; + +my @examples = + ( [ 1, 3, 4, 5, 7 ], [ 1, 2, 3, 6, 7, 9 ], [ 0, 1, 2, 4, 5, 6, 8, 9 ], ); + +for my $e (@examples) { + my @array = $e->@*; + my @output = range_list(@array); + my $array = join ', ', @array; + my $output = join ', ', + map { qq{[$_]} } map { join ', ', $_->@* } @output; + say <<"END"; + Input: \@array = ($array) + Output: $output +END +} + +sub range_list ( @array ) { + my @output; + my $k = 0; +OUTER: for my $i ( 0 .. -1 + scalar @array ) { + next if $i < $k; + my @block = ( $array[$i], -1 ); + for my $j ( $i + 1 .. -1 + scalar @array ) { + if ( $array[ $j - 1 ] + 1 == $array[$j] ) { + $block[1] = $array[$j]; + } + else { + $k = $j; + next OUTER; + } + push @output, \@block; + } + } + return map { [ split /,/, $_ ] } uniq map { join ',', $_->@* } @output; +} |
