aboutsummaryrefslogtreecommitdiff
path: root/challenge-092
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-12-27 09:14:08 +0000
committerGitHub <noreply@github.com>2020-12-27 09:14:08 +0000
commita1e00dbf4f6137980589e59b31cfcb6e21e27479 (patch)
tree42604d3144700d6273b49adbc2341162e295daea /challenge-092
parentca6c62792484ec1fa6ad270e670dfcbd4f95cb19 (diff)
parent9a6f0865ce5d3a7fbf66d851715c5a8271654fba (diff)
downloadperlweeklychallenge-club-a1e00dbf4f6137980589e59b31cfcb6e21e27479.tar.gz
perlweeklychallenge-club-a1e00dbf4f6137980589e59b31cfcb6e21e27479.tar.bz2
perlweeklychallenge-club-a1e00dbf4f6137980589e59b31cfcb6e21e27479.zip
Merge pull request #3076 from gugod/gugod-092
@gugod's solution to pwc 092
Diffstat (limited to 'challenge-092')
-rw-r--r--challenge-092/gugod/README5
-rw-r--r--challenge-092/gugod/blog.txt1
-rw-r--r--challenge-092/gugod/blog1.txt1
-rw-r--r--challenge-092/gugod/raku/ch-1.raku45
-rw-r--r--challenge-092/gugod/raku/ch-2.raku44
5 files changed, 91 insertions, 5 deletions
diff --git a/challenge-092/gugod/README b/challenge-092/gugod/README
index 552c5afaa1..509fd4c50c 100644
--- a/challenge-092/gugod/README
+++ b/challenge-092/gugod/README
@@ -1,6 +1 @@
Solutions by Kang-min Liu.
-
-Blog posts:
-- https://gugod.org/2020/11/pwc-088/
-- https://gugod.org/2020/11/pwc-088-en/
-- https://dev.to/gugod/solving-perl-weekly-challenge-088-array-of-prodict-vs-spiral-matrix-13ni
diff --git a/challenge-092/gugod/blog.txt b/challenge-092/gugod/blog.txt
new file mode 100644
index 0000000000..f56bb9a100
--- /dev/null
+++ b/challenge-092/gugod/blog.txt
@@ -0,0 +1 @@
+https://gugod.org/2020/12/pwc-092/
diff --git a/challenge-092/gugod/blog1.txt b/challenge-092/gugod/blog1.txt
new file mode 100644
index 0000000000..fc96ec50b4
--- /dev/null
+++ b/challenge-092/gugod/blog1.txt
@@ -0,0 +1 @@
+https://gugod.org/2020/12/pwc-092-en/
diff --git a/challenge-092/gugod/raku/ch-1.raku b/challenge-092/gugod/raku/ch-1.raku
new file mode 100644
index 0000000000..2bb2dadf14
--- /dev/null
+++ b/challenge-092/gugod/raku/ch-1.raku
@@ -0,0 +1,45 @@
+
+sub MAIN {
+ tests;
+}
+
+sub isomorphic (Str $A, Str $B) {
+ my %trans;
+ return False unless $A.chars == $B.chars;
+
+ for $A.comb Z $B.comb -> ($a, $b) {
+ if %trans{"ab"}{$a}:exists and %trans{"ab"}{$a} ne $b {
+ return False
+ }
+ %trans{"ab"}{$a} = $b;
+
+ if %trans{"ba"}{$b}:exists and %trans{"ba"}{$b} ne $a {
+ return False
+ }
+ %trans{"ba"}{$b} = $a;
+ }
+ return True;
+}
+
+sub tests {
+ my @cases := (
+ (True, "abc", "xyz"),
+ (True, "abb", "xyy"),
+ (True, "look", "moon"),
+ (True, "sum", "abc"),
+
+ (False, "where", "how"),
+ (False, "this", "that"),
+ (False, "sum", "add"),
+ (False, "sun", "moon"),
+ (False, "Moose", "Mouse"),
+ );
+
+ for @cases -> ($expected, $a, $b) {
+ if $expected eqv isomorphic($a, $b) {
+ say "ok - $a vs $b";
+ } else {
+ say "not ok - $a vs $b"
+ }
+ }
+}
diff --git a/challenge-092/gugod/raku/ch-2.raku b/challenge-092/gugod/raku/ch-2.raku
new file mode 100644
index 0000000000..d863448b48
--- /dev/null
+++ b/challenge-092/gugod/raku/ch-2.raku
@@ -0,0 +1,44 @@
+sub MAIN {
+
+ my @cases = (
+ (((1,4), (8,10)), (2,6)),
+ (((1,2), (3,7), (8,10)), (5,8)),
+ (((1,5), (7,9)), (10,11)),
+ (((1,2), (3,7), (8,10)), (2,8)),
+ );
+
+ for @cases -> $it {
+ my $S = $it[0];
+ my $N = $it[1];
+ my $S2 = insert-intervals($S, $N);
+ say $S2.gist ~ " <- " ~ $S.gist ~ " (+) " ~ $N.gist;
+ }
+}
+
+sub insert-intervals (@S is copy, $N) {
+ my $i = @S.first(-> $s { $s[0] <= $N[0] <= $s[1] }, :k);
+ my $j = @S.first(-> $s { $s[0] <= $N[1] <= $s[1] }, :k);
+
+ if $i.defined {
+ @S[$i] = (@S[$i][0], max(@S[$i][1], $N[1]));
+ }
+
+ if $j.defined {
+ @S[$j] = (@S[$j][0], max(@S[$j][1], $N[1]));
+
+ if $i.defined and @S[$i][0] <= @S[$j][0] <= @S[$i][1] {
+ @S[$i] = (@S[$i][0], @S[$j][1]);
+
+ for $i^..$j -> $x {
+ @S[$x] = Nil;
+ }
+ }
+ }
+
+ if none($i.defined, $j.defined) {
+ @S.push($N);
+ @S = @S.sort({ $^a[0] <=> $^b[0] });
+ }
+
+ return @S.grep({ .defined });
+}