From 1b357ad56b14d989d10b77d6e558535fcbd70fa8 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 23 Jan 2023 18:44:41 +0100 Subject: Solution to task 1 --- challenge-201/jo-37/perl/ch-1.pl | 96 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100755 challenge-201/jo-37/perl/ch-1.pl diff --git a/challenge-201/jo-37/perl/ch-1.pl b/challenge-201/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..6ef767bbc8 --- /dev/null +++ b/challenge-201/jo-37/perl/ch-1.pl @@ -0,0 +1,96 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use List::Util 'sum'; +use experimental 'postderef'; +use Benchmark 'cmpthese'; + +our ($tests, $examples, $benchmark, $single); + +run_tests() if $tests || $examples || $benchmark; # does not return + +die <@{0 .. @_} = (); + delete @num{@_}; + sort {$a <=> $b} keys %num; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is [find_missing(-single, 0, 1, 3)], [2], 'example 1, single'; + is [find_missing(undef, 0, 1, 3)], [2], 'example 1, multi'; + is [find_missing(-single, 0, 1)], [2], 'example 2, single'; + is [find_missing(undef, 0, 1)], [2], 'example 2, multi'; + } + + SKIP: { + skip "tests" unless $tests; + + is [find_missing(undef, 4, 7, -1)], [0 .. 3], 'nothing in list'; + } + + SKIP: { + skip "benchmark" unless $benchmark; + + is [find_missing(-single, 1 .. 1000000)], [0], 'sum'; + is [find_missing(undef, 1 .. 1000000)], [0], 'hash'; + + cmpthese(0, { + sum => sub {find_missing(-single, 1 .. 1000000)}, + hash => sub {find_missing(undef, 1 .. 1000000)}, + }); + } + + done_testing; + exit; +} + +__DATA__ + Rate hash sum +hash 1.47/s -- -97% +sum 48.2/s 3187% -- -- cgit From 0f3bbc3cbb52abe06d40f9db9a914a8a7c0337fc Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 23 Jan 2023 18:46:19 +0100 Subject: Solution to task 2 --- challenge-201/jo-37/perl/ch-2.sh | 4 ++++ 1 file changed, 4 insertions(+) create mode 100755 challenge-201/jo-37/perl/ch-2.sh diff --git a/challenge-201/jo-37/perl/ch-2.sh b/challenge-201/jo-37/perl/ch-2.sh new file mode 100755 index 0000000000..ba66afd8c3 --- /dev/null +++ b/challenge-201/jo-37/perl/ch-2.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +# Find the number of partitions +perl -MMath::Prime::Util=:all -E 'say partitions shift' $1 -- cgit