diff options
| author | James Smith <js5@sanger.ac.uk> | 2023-04-29 01:22:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-29 01:22:23 +0100 |
| commit | 2fce8357fa4323473b68c62fc9e30f2fa2e58576 (patch) | |
| tree | 366e6f58526eb24cea83aa14f4a3008ed2742454 | |
| parent | 0cdef6a3120e745077cf8c701440294d152e0eec (diff) | |
| download | perlweeklychallenge-club-2fce8357fa4323473b68c62fc9e30f2fa2e58576.tar.gz perlweeklychallenge-club-2fce8357fa4323473b68c62fc9e30f2fa2e58576.tar.bz2 perlweeklychallenge-club-2fce8357fa4323473b68c62fc9e30f2fa2e58576.zip | |
Create ch-2.pl
| -rw-r--r-- | challenge-214/james-smith/perl/ch-2.pl | 32 |
1 files changed, 32 insertions, 0 deletions
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(); |
