diff options
| author | James Smith <js5@sanger.ac.uk> | 2023-05-01 12:30:28 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-01 12:30:28 +0100 |
| commit | e008af14ea1b656efae1003a21fb8a760c4af78e (patch) | |
| tree | 6d6a216c5a2b99721d041c1f97b3b47913cdb1dd | |
| parent | 1f855572495837fbca94dda5f64741a3473d6f1b (diff) | |
| download | perlweeklychallenge-club-e008af14ea1b656efae1003a21fb8a760c4af78e.tar.gz perlweeklychallenge-club-e008af14ea1b656efae1003a21fb8a760c4af78e.tar.bz2 perlweeklychallenge-club-e008af14ea1b656efae1003a21fb8a760c4af78e.zip | |
Create ch-2.pl
| -rw-r--r-- | challenge-215/james-smith/perl/ch-2.pl | 30 |
1 files changed, 30 insertions, 0 deletions
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(); |
