From 8eb53567387f21ed55cec89e55e32027bea99b7a Mon Sep 17 00:00:00 2001 From: Aaron Smith Date: Mon, 29 Mar 2021 09:17:43 -0500 Subject: Challenge 106 - Raku --- challenge-106/aaronreidsmith/blog.txt | 1 + challenge-106/aaronreidsmith/raku/ch-1.raku | 31 +++++++++++++++++++++++++++++ challenge-106/aaronreidsmith/raku/ch-2.raku | 29 +++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 challenge-106/aaronreidsmith/blog.txt create mode 100644 challenge-106/aaronreidsmith/raku/ch-1.raku create mode 100644 challenge-106/aaronreidsmith/raku/ch-2.raku diff --git a/challenge-106/aaronreidsmith/blog.txt b/challenge-106/aaronreidsmith/blog.txt new file mode 100644 index 0000000000..eddaf26fff --- /dev/null +++ b/challenge-106/aaronreidsmith/blog.txt @@ -0,0 +1 @@ +https://aaronreidsmith.github.io/blog/perl-weekly-challenge-106/ diff --git a/challenge-106/aaronreidsmith/raku/ch-1.raku b/challenge-106/aaronreidsmith/raku/ch-1.raku new file mode 100644 index 0000000000..79ed497296 --- /dev/null +++ b/challenge-106/aaronreidsmith/raku/ch-1.raku @@ -0,0 +1,31 @@ +#!/usr/bin/env raku + +sub challenge(@N where all(@N) ~~ Int) returns Int { + if @N.elems == 1 { + 0; + } else { + my @sorted = @N.sort; + my @zipped = @sorted[0..*-1] Z @sorted[1..*]; + @zipped.map(-> ($a, $b) { abs($b - $a) }).max; + } +} + +multi sub MAIN(*@N where all(@N) ~~ Int) { + say challenge(@N); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + ((2, 9, 3, 5), 4), + ((1, 3, 8, 2, 0), 5), + ((5,), 0) + ); + + for @tests -> (@N, $expected) { + is(challenge(@N), $expected); + } + + done-testing; +} diff --git a/challenge-106/aaronreidsmith/raku/ch-2.raku b/challenge-106/aaronreidsmith/raku/ch-2.raku new file mode 100644 index 0000000000..807773216a --- /dev/null +++ b/challenge-106/aaronreidsmith/raku/ch-2.raku @@ -0,0 +1,29 @@ +#!/usr/bin/env raku + +sub challenge(Numeric $N, Numeric $D) returns Str { + my ($base, $repeating) = ($N / $D).base-repeating; + $repeating = $repeating eq '' ?? $repeating !! "\($repeating\)"; + $base ~ $repeating; +} + +multi sub MAIN(Numeric $N, Numeric $D) { + say challenge($N, $D); +} + +multi sub MAIN(Bool :$test) { + use Test; + + my @tests = ( + (1, 3, '0.(3)'), + (1, 2, '0.5'), + (5, 66, '0.0(75)'), + (1, 7, '0.(142857)'), + (1.1, 8.5, '0.1(2941176470588235)') # Should handle arbitrary numerics, if you're into that + ); + + for @tests -> ($N, $D, $expected) { + is(challenge($N, $D), $expected); + } + + done-testing; +} -- cgit