diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-04-30 23:16:29 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-05-01 22:10:02 +0200 |
| commit | 360dd21eeecc7276b8fa0c6dd7dc4412fe3eeb96 (patch) | |
| tree | 4abc13b98daa7b76f37e35527179309c9a6a07fd | |
| parent | dd95a52513c3e127588be0a077347ad8cd9f9882 (diff) | |
| download | perlweeklychallenge-club-360dd21eeecc7276b8fa0c6dd7dc4412fe3eeb96.tar.gz perlweeklychallenge-club-360dd21eeecc7276b8fa0c6dd7dc4412fe3eeb96.tar.bz2 perlweeklychallenge-club-360dd21eeecc7276b8fa0c6dd7dc4412fe3eeb96.zip | |
Challenge 146 task 2
| -rwxr-xr-x | challenge-146/jo-37/perl/ch-2.pl | 69 |
1 files changed, 69 insertions, 0 deletions
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 <<EOS unless @ARGV == 1; +usage: $0 [-examples] [-tests] [D/N] + +-examples + run the examples from the challenge + +-tests + run some tests + +D/N + fraction + +EOS + + +### Input and Output + +main: { + my @parents; + push @parents, curious_parent(split m{/}, shift, 2); + push @parents, curious_parent($parents[-1]->@*); + 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; +} |
