From e008af14ea1b656efae1003a21fb8a760c4af78e Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 1 May 2023 12:30:28 +0100 Subject: Create ch-2.pl --- challenge-215/james-smith/perl/ch-2.pl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 challenge-215/james-smith/perl/ch-2.pl diff --git a/challenge-215/james-smith/perl/ch-2.pl b/challenge-215/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..f01950dbd8 --- /dev/null +++ b/challenge-215/james-smith/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use feature qw(say); +use Test::More; + +my @TESTS = ( + [ [ 1, [1,0,0,0,1] ], 1 ], + [ [ 2, [1,0,0,0,1] ], 0 ], + [ [ 3, [1,0,0,0,0,0,0,0,1] ], 1 ], + [ [ 3, [1,0,0,0,0,0,0,0] ], 1], +); + +sub insert_zero { + my($s,$c) = (0,shift); + $_ ? ( $c-= $s>2 && int( ($s-1)/2 ), $s=0 ) : $s++ for @_,1; + $c>0?0:1; +} + +sub insert_zero_simultaneous { + my($s,$c) = (0,shift); + $_ ? ( $c-= $s>2 && $s-2, $s=0 ) : $s++ for @_,1; + $c>0?0:1 +} + +is( insert_zero( $_->[0][0], @{$_->[0][1]} ), $_->[1] ) for @TESTS; +is( insert_zero_simultaneous( $_->[0][0], @{$_->[0][1]} ), $_->[1] ) for @TESTS; + +done_testing(); -- cgit