diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-21 23:11:30 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-21 23:11:30 +0000 |
| commit | d58f57e2ed5036d9a2db66c9d656dbbad55d1566 (patch) | |
| tree | fe6dc24ac058e2d7926e2f6ca32ee4002867fa09 /challenge-252/barroff/perl/ch-2.pl | |
| parent | 899fd7e7fca7954906336197f81f6bef1b82143e (diff) | |
| parent | 9e95efd240594c350588fe50cbd69cf9363490a4 (diff) | |
| download | perlweeklychallenge-club-d58f57e2ed5036d9a2db66c9d656dbbad55d1566.tar.gz perlweeklychallenge-club-d58f57e2ed5036d9a2db66c9d656dbbad55d1566.tar.bz2 perlweeklychallenge-club-d58f57e2ed5036d9a2db66c9d656dbbad55d1566.zip | |
Merge pull request #9439 from BarrOff/barroff-252
feat: add solutions for challenge 252 from BarrOff
Diffstat (limited to 'challenge-252/barroff/perl/ch-2.pl')
| -rw-r--r-- | challenge-252/barroff/perl/ch-2.pl | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/challenge-252/barroff/perl/ch-2.pl b/challenge-252/barroff/perl/ch-2.pl new file mode 100644 index 0000000000..ecc45123ff --- /dev/null +++ b/challenge-252/barroff/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use v5.38; + +sub unique_sum_zero ($n) { + my @ints; + map( { push( @ints, $_, 0 - $_ ) } 1 .. int( $n / 2 ) ); + push( @ints, 0 ) if $n % 2 == 1; + @ints = sort( { $a <=> $b } @ints ); + return \@ints; +} + +sub MAIN() { + if (@ARGV) { + + #| Run on command line argument + say @{ unique_sum_zero( $ARGV[0] ) }; + } + else { + #| Run test cases + use Test2::V0 qw( is plan ); + plan 4; + + is unique_sum_zero(5), [ -2, -1, 0, 1, 2 ], 'works for 5'; + is unique_sum_zero(3), [ -1, 0, 1 ], 'works for 3'; + is unique_sum_zero(1), [0], 'works for 1'; + is unique_sum_zero(0), [], 'works for 0'; + } +} + +MAIN(); |
