diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-20 11:05:37 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-20 11:05:37 +0000 |
| commit | 9ea38fd04a87628ebec1a794428609f41db8b8b2 (patch) | |
| tree | dfc0a046415f9574593a6c81109d7e547da97a73 /challenge-252 | |
| parent | 24088341242396d730be696b80d2c4f6ac06c469 (diff) | |
| parent | 2084950ba8bca108d65373ba669cd71a147b722e (diff) | |
| download | perlweeklychallenge-club-9ea38fd04a87628ebec1a794428609f41db8b8b2.tar.gz perlweeklychallenge-club-9ea38fd04a87628ebec1a794428609f41db8b8b2.tar.bz2 perlweeklychallenge-club-9ea38fd04a87628ebec1a794428609f41db8b8b2.zip | |
Merge pull request #9425 from jo-37/contrib
Solutions to challenge 252
Diffstat (limited to 'challenge-252')
| -rw-r--r-- | challenge-252/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-252/jo-37/perl/ch-1.pl | 58 | ||||
| -rwxr-xr-x | challenge-252/jo-37/perl/ch-2.pl | 67 |
3 files changed, 126 insertions, 0 deletions
diff --git a/challenge-252/jo-37/blog.txt b/challenge-252/jo-37/blog.txt new file mode 100644 index 0000000000..c7539cbf2b --- /dev/null +++ b/challenge-252/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/blog/pwc/challenge-252 diff --git a/challenge-252/jo-37/perl/ch-1.pl b/challenge-252/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..0b1cc1f5c2 --- /dev/null +++ b/challenge-252/jo-37/perl/ch-1.pl @@ -0,0 +1,58 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; +use PDL::NiceSlice; +use Math::Prime::Util 'divisors'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [I...] + +-examples + run the examples from the challenge + +-tests + run some tests + +I... + list of integers + +EOS + + +### Input and Output + +say snss(@ARGV); + + +### Implementation + +sub snss { + sum pdl(@_)->(indx(divisors @_) - 1) ** 2 +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is snss(1, 2, 3, 4), 21, 'example 1'; + is snss(2, 7, 1, 19, 18, 3), 63, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + is snss(-1, -2), 5, 'negative values'; + } + + done_testing; + exit; +} 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; +} |
