From 360dd21eeecc7276b8fa0c6dd7dc4412fe3eeb96 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Sun, 30 Apr 2023 23:16:29 +0200 Subject: Challenge 146 task 2 --- challenge-146/jo-37/perl/ch-2.pl | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100755 challenge-146/jo-37/perl/ch-2.pl diff --git a/challenge-146/jo-37/perl/ch-2.pl b/challenge-146/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..aedafe7fdd --- /dev/null +++ b/challenge-146/jo-37/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use experimental 'signatures'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <@*); + local $, = ' '; + say map {join '/', @$_} @parents; +} + + +### Implementation + +# The child nodes are made from the parent's +# - numerator and numerator + denominator +# - numerator + denominator and denominator +# In reverse, the parent depends on the order of the child's denominator +# and numerator and may be reconstructed easily. +sub curious_parent ($d, $n) { + $d < $n ? [$d, $n - $d] : [$d - $n, $n]; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + my $parent = curious_parent(3, 5); + is $parent, [3, 2], 'example 1 parent'; + is curious_parent(@$parent), [1, 2], 'example 1 grandparent'; + + $parent = curious_parent(4, 3); + is $parent, [1, 3], 'example 2 parent'; + is curious_parent(@$parent), [1, 2], 'example 2 grandparent'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} -- cgit