From 2fce8357fa4323473b68c62fc9e30f2fa2e58576 Mon Sep 17 00:00:00 2001 From: James Smith Date: Sat, 29 Apr 2023 01:22:23 +0100 Subject: Create ch-2.pl --- challenge-214/james-smith/perl/ch-2.pl | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 challenge-214/james-smith/perl/ch-2.pl diff --git a/challenge-214/james-smith/perl/ch-2.pl b/challenge-214/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..275e2aff92 --- /dev/null +++ b/challenge-214/james-smith/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use feature qw(say); +use Test::More; + +my @TESTS = ( + [ [2,4,3,3,3,4,5,4,2], 23 ], + [ [1,2,2,2,2,1], 20 ], + [ [1], 1 ], + [ [2,2,2,1,1,2,2,2], 40 ], + [ [2,1,2,1,2,1,2,1,2,1,2,1,2,1,2], 1 ], +); + +sub collect { + return 0 unless @_; + my $m = 0; + for ( my $e = my $o = 0; $o<@_; ) { + my $e = $o; + $e++ while $_[$o]==$_[$e]; + sub { $m=$_[0] if $m<$_[0] }->( + ($e-$o)**2 + + collect( @_[ 0..$o-1, $e..$#_ ] ) + ); + $o = $e + } + $m +} + +is( collect( @{$_->[0]} ), $_->[1] ) for @TESTS; +done_testing(); -- cgit