aboutsummaryrefslogtreecommitdiff
path: root/challenge-092
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-12-27 08:21:37 +0000
committerGitHub <noreply@github.com>2020-12-27 08:21:37 +0000
commitff30a91c0c6c482cf9ae1044b9fb53fafcd5adb3 (patch)
treef26efe8a93283c7f02240dc445eced0c03cf6b67 /challenge-092
parent16a91bcbabf2dc4d1e5dcaf53d7a45f2f73e7ae1 (diff)
parent5300ddb767f9f83e3ca22baba9979b59bd1db16c (diff)
downloadperlweeklychallenge-club-ff30a91c0c6c482cf9ae1044b9fb53fafcd5adb3.tar.gz
perlweeklychallenge-club-ff30a91c0c6c482cf9ae1044b9fb53fafcd5adb3.tar.bz2
perlweeklychallenge-club-ff30a91c0c6c482cf9ae1044b9fb53fafcd5adb3.zip
Merge pull request #3069 from aaronreidsmith/challenge-092
Challenge 92 - Raku
Diffstat (limited to 'challenge-092')
-rw-r--r--challenge-092/aaronreidsmith/blog.txt1
-rw-r--r--challenge-092/aaronreidsmith/raku/ch-1.raku40
-rw-r--r--challenge-092/aaronreidsmith/raku/ch-2.raku50
3 files changed, 91 insertions, 0 deletions
diff --git a/challenge-092/aaronreidsmith/blog.txt b/challenge-092/aaronreidsmith/blog.txt
new file mode 100644
index 0000000000..90d77ef69c
--- /dev/null
+++ b/challenge-092/aaronreidsmith/blog.txt
@@ -0,0 +1 @@
+https://aaronreidsmith.github.io/blog/perl-weekly-challenge-092/
diff --git a/challenge-092/aaronreidsmith/raku/ch-1.raku b/challenge-092/aaronreidsmith/raku/ch-1.raku
new file mode 100644
index 0000000000..f78ca0dca7
--- /dev/null
+++ b/challenge-092/aaronreidsmith/raku/ch-1.raku
@@ -0,0 +1,40 @@
+#!/usr/bin/env raku
+
+use MONKEY-TYPING;
+
+augment class Seq {
+ method OrderedSet {
+ my @set;
+ for self -> $item {
+ next if $item ∈ @set;
+ @set.push($item);
+ }
+ @set;
+ }
+}
+
+sub challenge(Str $A, Str $B) returns Int {
+ my $a-chars = $A.comb.OrderedSet.join;
+ my $b-chars = $B.comb.OrderedSet.join;
+ ($A.trans($a-chars => $b-chars) eq $B).Int;
+}
+
+multi sub MAIN(Str $A, Str $B) {
+ say challenge($A, $B);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ ('abc', 'xyz', 1),
+ ('abb', 'xyy', 1),
+ ('sum', 'add', 0)
+ );
+
+ for @tests -> @test {
+ is(challenge(@test[0], @test[1]), @test[2]);
+ }
+
+ done-testing;
+}
diff --git a/challenge-092/aaronreidsmith/raku/ch-2.raku b/challenge-092/aaronreidsmith/raku/ch-2.raku
new file mode 100644
index 0000000000..9c3383c918
--- /dev/null
+++ b/challenge-092/aaronreidsmith/raku/ch-2.raku
@@ -0,0 +1,50 @@
+#!/usr/bin/env raku
+
+subset MultiRange of Str where { $_ ~~ /^[<digit>+'-'<digit>+',']+[<digit>+'-'<digit>+]$/ }
+subset Range of Str where { $_ ~~ /^<digit>+'-'<digit>+$/ }
+
+sub challenge(@S, $N) {
+ my @combined = (|@S, $N).sort({ $^a.min cmp $^b.min });
+ my @final = (@combined.head,);
+ OUT: for @combined[1..*] -> $new-range {
+ my ($min, $max) = $new-range.minmax;
+ IN: for @final.kv -> $index, $range {
+ next IN if $min ∈ $range && $max ∈ $range;
+ if $min ∈ $range {
+ @final[$index] = ($range.min..$max);
+ next OUT;
+ } elsif $max ∈ $range {
+ @final[$index] = ($min..$range.max);
+ next OUT;
+ } elsif $index == @final.end {
+ @final.push($new-range);
+ }
+ }
+ }
+ @final.map(-> $range { "({$range.min},{$range.max})" }).join(', ');
+}
+
+multi sub MAIN(MultiRange :$S, Range :$N) {
+ my @s = $S.split(',').map(*.split('-').map(*.Int).minmax);
+ my $n = $N.split('-').map(*.Int).minmax;
+ say challenge(@s, $n);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ ((1..4), (8..10), (2..6), '(1,6), (8,10)'),
+ ((1..2), (3..7), (8..10), (5..8), '(1,2), (3,10)'),
+ ((1..5), (7..9), (10..11), '(1,5), (7,9), (10,11)')
+ );
+
+ for @tests -> @test {
+ my @S = @test[0..^*-2];
+ my $N = @test[*-2];
+ my $answer = @test[*-1];
+ is(challenge(@S, $N), $answer);
+ }
+
+ done-testing;
+}