aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-03-30 02:27:40 +0100
committerGitHub <noreply@github.com>2021-03-30 02:27:40 +0100
commitb0eb5932d6877291ada2d227b0b314bbd886218b (patch)
treef3fa96bb01067180847ec3828d1d3437fbc78c76
parentb3de1a914296c86594e9ce88bf707bf98ec7148f (diff)
parent8eb53567387f21ed55cec89e55e32027bea99b7a (diff)
downloadperlweeklychallenge-club-b0eb5932d6877291ada2d227b0b314bbd886218b.tar.gz
perlweeklychallenge-club-b0eb5932d6877291ada2d227b0b314bbd886218b.tar.bz2
perlweeklychallenge-club-b0eb5932d6877291ada2d227b0b314bbd886218b.zip
Merge pull request #3799 from aaronreidsmith/challenge-106
Challenge 106 - Raku
-rw-r--r--challenge-106/aaronreidsmith/blog.txt1
-rw-r--r--challenge-106/aaronreidsmith/raku/ch-1.raku31
-rw-r--r--challenge-106/aaronreidsmith/raku/ch-2.raku29
3 files changed, 61 insertions, 0 deletions
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;
+}