diff options
| author | James Smith <js5@sanger.ac.uk> | 2023-03-27 11:27:34 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-27 11:27:34 +0100 |
| commit | 029680e3829388f61ba89cef6ec43f1cff14ba98 (patch) | |
| tree | 63f3588afe0af75443545bc02df00195820ad921 | |
| parent | 259ce124ace36cff23513157b49ecafb9d0da1fd (diff) | |
| download | perlweeklychallenge-club-029680e3829388f61ba89cef6ec43f1cff14ba98.tar.gz perlweeklychallenge-club-029680e3829388f61ba89cef6ec43f1cff14ba98.tar.bz2 perlweeklychallenge-club-029680e3829388f61ba89cef6ec43f1cff14ba98.zip | |
Create ch-2.pl
| -rw-r--r-- | challenge-210/james-smith/perl/ch-2.pl | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-210/james-smith/perl/ch-2.pl b/challenge-210/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..495cfe3845 --- /dev/null +++ b/challenge-210/james-smith/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use feature qw(say); +use Test::More; + +my @TESTS = ( + [ [2,3,-1] => '2 3' ], + [ [3,2,-4] => '-4' ], + [ [1,-1] => '' ], +); + +sub collision { + my @st; + $_[0]>0 || !@st || $st[-1] < 0 ? ( push @st, shift ) + ## +ve no, empty stack or last stack is -ve + ## we keep this at the moment so push to stack + : $st[-1] == -$_[0] ? ( pop @st, shift ) + ## -ve no and equal in absolute value + ## remove +ve value from stack and drop + ## current value + : $st[-1] >= -$_[0] ? ( shift ) + ## -ve no and smaller in abs value + ## drop current value + : ( pop @st ) + ## -ve no and greater in abs value + ## remove previous number from stack + while @_; + @st ## return value +} + +is( "@{[ collision( @{$_->[0]} ) ]}", $_->[1] ) for @TESTS; + +## Without comments... + +sub nc_collision { + my @st; + $_[0]>0 || !@st || $st[-1] < 0 ? push @st, shift + : $st[-1] == -$_[0] ? pop @st && shift + : $st[-1] >= -$_[0] ? shift + : pop @st + while @_; + @st +} |
