aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-06-08 09:46:34 +0100
committerGitHub <noreply@github.com>2021-06-08 09:46:34 +0100
commit3b2973503480b78d63c8f03fa94802605508becc (patch)
treebd2ca4229f2dc1273d0a88f557d6c2c2169a9978
parent4b6f27e088b081192ca26c7ed0098feead260a03 (diff)
parentca99430cc169bc78ff5cefdd59724c78bcdd955f (diff)
downloadperlweeklychallenge-club-3b2973503480b78d63c8f03fa94802605508becc.tar.gz
perlweeklychallenge-club-3b2973503480b78d63c8f03fa94802605508becc.tar.bz2
perlweeklychallenge-club-3b2973503480b78d63c8f03fa94802605508becc.zip
Merge pull request #4224 from aaronreidsmith/challenge-115
Challenges 115 & 116 - Raku
-rw-r--r--challenge-115/aaronreidsmith/blog.txt1
-rw-r--r--challenge-115/aaronreidsmith/raku/ch-1.raku32
-rw-r--r--challenge-115/aaronreidsmith/raku/ch-2.raku25
-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
6 files changed, 140 insertions, 0 deletions
diff --git a/challenge-115/aaronreidsmith/blog.txt b/challenge-115/aaronreidsmith/blog.txt
new file mode 100644
index 0000000000..c6fd3c9318
--- /dev/null
+++ b/challenge-115/aaronreidsmith/blog.txt
@@ -0,0 +1 @@
+https://aaronreidsmith.github.io/blog/perl-weekly-challenge-115/
diff --git a/challenge-115/aaronreidsmith/raku/ch-1.raku b/challenge-115/aaronreidsmith/raku/ch-1.raku
new file mode 100644
index 0000000000..b9a1d0813f
--- /dev/null
+++ b/challenge-115/aaronreidsmith/raku/ch-1.raku
@@ -0,0 +1,32 @@
+#!/usr/bin/env raku
+
+sub challenge(@S) returns Int {
+ my @solutions = @S.race.permutations.grep: -> @permutation {
+ my $valid = True;
+ for @permutation Z (|@permutation[1..*], @permutation.head) -> ($a, $b) {
+ if $a.comb.tail ne $b.comb.head {
+ $valid = False;
+ last;
+ }
+ }
+ $valid;
+ }
+ (@solutions.elems > 0).Int;
+}
+
+multi sub MAIN(*@S where all(@S) ~~ Str) {
+ say challenge(@S);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ (('abc', 'dea', 'cd'), 1),
+ (('ade', 'cbd', 'fgh'), 0)
+ );
+
+ for @tests -> (@S, $expected) {
+ is(challenge(@S), $expected);
+ }
+}
diff --git a/challenge-115/aaronreidsmith/raku/ch-2.raku b/challenge-115/aaronreidsmith/raku/ch-2.raku
new file mode 100644
index 0000000000..2f3dd8f59a
--- /dev/null
+++ b/challenge-115/aaronreidsmith/raku/ch-2.raku
@@ -0,0 +1,25 @@
+#!/usr/bin/env raku
+
+sub challenge(@N) returns Int {
+ @N.race.permutations.map(*.join.Int).grep(* %% 2).max;
+}
+
+multi sub MAIN(*@N where all(@N) ~~ /^<digit>$/) {
+ say challenge(@N);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ ((1, 0, 2, 6), 6210),
+ ((1, 4, 2, 8), 8412),
+ ((4, 1, 7, 6), 7614)
+ );
+
+ for @tests -> (@N, $expected) {
+ is(challenge(@N), $expected);
+ }
+
+ done-testing;
+}
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;
+}