aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Smith <aaronreidsmith@gmail.com>2021-06-07 11:00:59 -0500
committerAaron Smith <aaronreidsmith@gmail.com>2021-06-07 11:00:59 -0500
commitca99430cc169bc78ff5cefdd59724c78bcdd955f (patch)
treefea9522997b77e22dc838f3bff6df96f8056ccb2
parent51ac7e4efc83b17e3162b31e9201fad7706d4f81 (diff)
downloadperlweeklychallenge-club-ca99430cc169bc78ff5cefdd59724c78bcdd955f.tar.gz
perlweeklychallenge-club-ca99430cc169bc78ff5cefdd59724c78bcdd955f.tar.bz2
perlweeklychallenge-club-ca99430cc169bc78ff5cefdd59724c78bcdd955f.zip
Challenge 116 - Raku
-rw-r--r--challenge-116/aaronreidsmith/blog.txt1
-rw-r--r--challenge-116/aaronreidsmith/raku/ch-1.raku55
-rw-r--r--challenge-116/aaronreidsmith/raku/ch-2.raku26
3 files changed, 82 insertions, 0 deletions
diff --git a/challenge-116/aaronreidsmith/blog.txt b/challenge-116/aaronreidsmith/blog.txt
new file mode 100644
index 0000000000..b790b1478f
--- /dev/null
+++ b/challenge-116/aaronreidsmith/blog.txt
@@ -0,0 +1 @@
+https://aaronreidsmith.github.io/blog/perl-weekly-challenge-116/
diff --git a/challenge-116/aaronreidsmith/raku/ch-1.raku b/challenge-116/aaronreidsmith/raku/ch-1.raku
new file mode 100644
index 0000000000..3812c95eb2
--- /dev/null
+++ b/challenge-116/aaronreidsmith/raku/ch-1.raku
@@ -0,0 +1,55 @@
+#!/usr/bin/env raku
+
+# Raku adaptation of Python's more-itertools.partitions: https://git.io/JZL8Q
+sub partitions(Str $S) {
+ my @sequence = $S.comb;
+ my $n = @sequence.elems;
+ my @partitions = gather for (1..^$n).combinations -> @combination {
+ my @partition = gather for (0, |@combination) Z (|@combination, $n) -> ($i, $j) {
+ take [@sequence[$i..^$j]];
+ }
+ take @partition;
+ }
+ gather for @partitions -> @partition {
+ # Filter out elements with leading zeros
+ my @invalid = @partition.grep(*.head eq '0');
+ if @invalid.elems == 0 {
+ take @partition.map(*.join.Int);
+ }
+ }
+}
+
+sub challenge(Int $N where $N >= 10) returns Str {
+ my $S = $N.Str;
+ my $solution = partitions($S).first: -> @partition {
+ my @zipped = @partition[0..*-1] Z @partition[1..*];
+ my @filtered = @zipped.grep(-> ($a, $b) { $b - $a == 1 });
+ @zipped.elems > 0 && @zipped.elems == @filtered.elems;
+ }
+
+ with $solution {
+ $solution.join(',');
+ } else {
+ $S
+ }
+}
+
+multi sub MAIN(Int $N) {
+ say challenge($N);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ (1234, '1,2,3,4'),
+ (91011, '9,10,11'),
+ (10203, '10203')
+ );
+
+ for @tests -> ($N, $expected) {
+ is(challenge($N), $expected);
+ }
+
+ done-testing;
+}
diff --git a/challenge-116/aaronreidsmith/raku/ch-2.raku b/challenge-116/aaronreidsmith/raku/ch-2.raku
new file mode 100644
index 0000000000..4f605ff5ec
--- /dev/null
+++ b/challenge-116/aaronreidsmith/raku/ch-2.raku
@@ -0,0 +1,26 @@
+#!/usr/bin/env raku
+
+sub challenge(Int $N where $N >= 10) returns Int {
+ my $square-sum = $N.comb.map(*²).sum;
+ $square-sum.sqrt.narrow ~~ Int ?? 1 !! 0;
+}
+
+multi sub MAIN(Int $N) {
+ say challenge($N);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ (34, 1),
+ (50, 1),
+ (52, 0)
+ );
+
+ for @tests -> ($N, $expected) {
+ is(challenge($N), $expected);
+ }
+
+ done-testing;
+}