aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-01-15 19:17:43 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-01-15 19:17:43 +0100
commitea3aae0871d1d8a10ac7458b812e59650149e5fa (patch)
tree04971082655f3981fd08f6c65eacb2da39077b52
parentb658b63de57d6dafea54b9d5d9f311f9d22ba9ed (diff)
downloadperlweeklychallenge-club-ea3aae0871d1d8a10ac7458b812e59650149e5fa.tar.gz
perlweeklychallenge-club-ea3aae0871d1d8a10ac7458b812e59650149e5fa.tar.bz2
perlweeklychallenge-club-ea3aae0871d1d8a10ac7458b812e59650149e5fa.zip
Solution to task 2
-rwxr-xr-xchallenge-252/jo-37/perl/ch-2.pl67
1 files changed, 67 insertions, 0 deletions
diff --git a/challenge-252/jo-37/perl/ch-2.pl b/challenge-252/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..7269103def
--- /dev/null
+++ b/challenge-252/jo-37/perl/ch-2.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl -s
+
+use v5.26;
+use Test2::V0;
+use experimental 'signatures';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [N]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N
+ positive integer
+
+EOS
+
+
+### Input and Output
+
+say "(@{[uniq_sum_zero(shift)]})";
+
+
+### Implementation
+
+sub uniq_sum_zero ($n) {
+ (-$n / 2 .. -1, (0) x ($n % 2), 1 .. $n / 2);
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ use List::Util qw(uniq sum);
+
+ my sub test_usz {
+ plan 2;
+ is scalar(uniq @_), scalar @_, 'uniq';
+ is sum(@_), 0, 'sum zero';
+ }
+
+ SKIP: {
+ skip "examples" unless $examples;
+
+ subtest 'example 1', \&test_usz, uniq_sum_zero(5);
+ subtest 'example 2', \&test_usz, uniq_sum_zero(3);
+ subtest 'example 3', \&test_usz, uniq_sum_zero(1);
+
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ subtest 'test 2', \&test_usz, uniq_sum_zero(2);
+ subtest 'test 4', \&test_usz, uniq_sum_zero(4);
+ }
+
+ done_testing;
+ exit;
+}