aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2021-02-22 22:25:54 +0800
committer冯昶 <fengchang@novel-supertv.com>2021-02-22 22:25:54 +0800
commit3cc47d00aee13d50c872499cecf574539a4cbe2a (patch)
tree77b6f6a1e8cae075e3c70fa1b4f82e85aa390743
parent572994875f5972916deabeb84f3648ac4640107b (diff)
downloadperlweeklychallenge-club-3cc47d00aee13d50c872499cecf574539a4cbe2a.tar.gz
perlweeklychallenge-club-3cc47d00aee13d50c872499cecf574539a4cbe2a.tar.bz2
perlweeklychallenge-club-3cc47d00aee13d50c872499cecf574539a4cbe2a.zip
challenge 101, raku solutions
-rwxr-xr-xchallenge-044/feng-chang/ch-1.raku14
-rwxr-xr-xchallenge-075/feng-chang/raku/ch-2.raku.local13
-rwxr-xr-xchallenge-091/feng-chang/raku/ch-1.raku14
-rwxr-xr-xchallenge-091/feng-chang/raku/ch-2.raku4
-rwxr-xr-xchallenge-096/feng-chang/ch-1.raku4
-rwxr-xr-xchallenge-096/feng-chang/ch-2.raku27
-rwxr-xr-xchallenge-101/feng-chang/raku/ch-1.raku61
-rwxr-xr-xchallenge-101/feng-chang/raku/ch-2.raku23
8 files changed, 160 insertions, 0 deletions
diff --git a/challenge-044/feng-chang/ch-1.raku b/challenge-044/feng-chang/ch-1.raku
new file mode 100755
index 0000000000..6cf53261a6
--- /dev/null
+++ b/challenge-044/feng-chang/ch-1.raku
@@ -0,0 +1,14 @@
+#!/bin/env raku
+
+use MONKEY-SEE-NO-EVAL;
+
+my @even = 0, 2 ... 16;
+my @odd = 1, 3 ... 15;
+
+([X] (['', '+', '-'] xx 8)).hyper(degree => 12).map({
+ my @a;
+ @a[@even] = '123456789'.comb;
+ @a[@odd] = $_;
+ my $s = @a.join;
+ put $s if EVAL($s) == 100;
+});
diff --git a/challenge-075/feng-chang/raku/ch-2.raku.local b/challenge-075/feng-chang/raku/ch-2.raku.local
new file mode 100755
index 0000000000..d64b758512
--- /dev/null
+++ b/challenge-075/feng-chang/raku/ch-2.raku.local
@@ -0,0 +1,13 @@
+#!/bin/env raku
+
+sub largest-rectangle-histogram(Int:D @A) {
+ my Int $n = @A.elems;
+ for 0..($n-1) -> $i {
+ for $i..($n-1) -> $j {
+ say min(|@A[$i..$j]) * ($j - $i + 1);
+ }
+ }
+}
+
+largest-rectangle-histogram(my Int @ = 2, 1, 4, 5, 3, 7);
+largest-rectangle-histogram(my Int @ = 3, 2, 3, 5, 7, 5);
diff --git a/challenge-091/feng-chang/raku/ch-1.raku b/challenge-091/feng-chang/raku/ch-1.raku
new file mode 100755
index 0000000000..a507ec3e5c
--- /dev/null
+++ b/challenge-091/feng-chang/raku/ch-1.raku
@@ -0,0 +1,14 @@
+#!/bin/env raku
+
+sub MAIN(UInt:D $N) {
+ my @n = $N.comb».UInt;
+
+ ((^@n.elems) X (^@n.elems)) # all possible ranges
+ .grep({ .[1] ≥ .[0] }) # end point of range should be greater or equal to start point
+ .grep({ [==] @n[.[0] .. .[1]] }) # all digits within the range should be the same
+ .grep({ .[0] == 0 or @n[.[0]-1] != @n[.[0]] }) # filter sub-ranges with left margin condition
+ .grep({ .[1] == @n.elems-1 or @n[.[1]] != @n[.[1]+1] }) # fliter sub-ranges with right margin condition
+ .map({ "{ .[1] - .[0] + 1 }{ @n[.[0]] }" }) # output each range
+ .join
+ .put;
+}
diff --git a/challenge-091/feng-chang/raku/ch-2.raku b/challenge-091/feng-chang/raku/ch-2.raku
new file mode 100755
index 0000000000..75af8104d2
--- /dev/null
+++ b/challenge-091/feng-chang/raku/ch-2.raku
@@ -0,0 +1,4 @@
+#!/bin/env raku
+
+sub MAIN(*@N) {
+}
diff --git a/challenge-096/feng-chang/ch-1.raku b/challenge-096/feng-chang/ch-1.raku
new file mode 100755
index 0000000000..a8271c123a
--- /dev/null
+++ b/challenge-096/feng-chang/ch-1.raku
@@ -0,0 +1,4 @@
+#!/bin/env raku
+
+multi MAIN(Str:D $S) { put $S.words.reverse.join(' ') }
+multi MAIN(*@S) { MAIN(@S.join(' ')) }
diff --git a/challenge-096/feng-chang/ch-2.raku b/challenge-096/feng-chang/ch-2.raku
new file mode 100755
index 0000000000..a84a74b7d7
--- /dev/null
+++ b/challenge-096/feng-chang/ch-2.raku
@@ -0,0 +1,27 @@
+#!/bin/env raku
+
+sub levenshtein-distance(Str:D $s, Str:D $t) {
+ my @S = $s.comb; my $m = @S.elems;
+ my @T = $t.comb; my $n = @T.elems;
+
+ my Array @a = (0 xx ($m+1)).Array xx ($n+1);
+ @a[0;1..$m] = 1..$m;
+ @a[1..$n;0] = 1..$n;
+
+ for ^$n -> $j {
+ for ^@S.elems -> $i {
+ my $subst-cost = @S[$i] eq @T[$j] ?? 0 !! 1;
+
+ @a[$j+1;$i+1] = min(
+ @a[$j;$i+1] + 1, # deletion
+ @a[$j+1;$i] + 1, # insertion
+ @a[$j;$i] + $subst-cost # substitution
+ );
+ }
+ }
+
+ @a[$n;$m], @a.item;
+}
+
+put levenshtein-distance('kitten', 'sitting')[0];
+put levenshtein-distance('Saturday', 'Sunday')[0];
diff --git a/challenge-101/feng-chang/raku/ch-1.raku b/challenge-101/feng-chang/raku/ch-1.raku
new file mode 100755
index 0000000000..5e76ef3cef
--- /dev/null
+++ b/challenge-101/feng-chang/raku/ch-1.raku
@@ -0,0 +1,61 @@
+#!/bin/env raku
+
+sub get-tight-mn(UInt:D \size --> List:D) {
+ (1..size X 1..size).grep({ .[0] ≤ .[1] and .[0] * .[1] == size}).min({ .[1] - .[0] });
+}
+
+sub tighten(*@A) {
+ my UInt ($h, $w) = get-tight-mn(@A.elems);
+ my @R;
+ @R[^$h;^$w] = '.' xx ∞;
+ my UInt ($y, $x) = ($h - 1, 0);
+ while @A.elems > 0 {
+ @R[$y;$x++] = $_ for @A.splice(0, $w);
+
+ --$x;
+ --$h;
+ last unless $h > 0;
+ @R[--$y;$x] = $_ for @A.splice(0, $h);
+
+ --$w;
+ last unless $w > 0;
+ @R[$y;--$x] = $_ for @A.splice(0, $w);
+
+ --$h;
+ last unless $h > 0;
+ @R[++$y;$x] = $_ for @A.splice(0, $h);
+
+ ++$x;
+ --$w;
+ }
+
+ @R;
+}
+
+sub show(*@A) {
+ my $width = @A[*;*]».chars.max;
+ put @A[$_]».fmt("%{ $width }s").join(' ') for ^@A.elems;
+}
+
+multi MAIN('test') {
+ use Test;
+
+ is-deeply get-tight-mn(6), (2, 3), 'get-tight-mn(6) => 2, 3';
+ is-deeply get-tight-mn(5), (1, 5), 'get-tight-mn(5) => 1, 5';
+ is-deeply get-tight-mn(9), (3, 3), 'get-tight-mn(9) => 3, 3';
+ is-deeply get-tight-mn(24), (4, 6), 'get-tight-mn(24) => 4, 6';
+
+ is-deeply tighten(1, 2, 3), [[1, 2, 3],], 'tighten(1..3) is correct';
+ is-deeply tighten(1..4), [[4, 3], [1, 2]], 'tighten(1..4) is correct';
+ is-deeply tighten(1..9), [[7, 6, 5], [8, 9, 4], [1, 2, 3]], 'tighten(1..9) is correct';
+
+ done-testing;
+}
+
+multi MAIN(UInt:D \n) {
+ show tighten(1..n);
+}
+
+multi MAIN(*@A) {
+ show tighten(@A);
+}
diff --git a/challenge-101/feng-chang/raku/ch-2.raku b/challenge-101/feng-chang/raku/ch-2.raku
new file mode 100755
index 0000000000..3e2823e892
--- /dev/null
+++ b/challenge-101/feng-chang/raku/ch-2.raku
@@ -0,0 +1,23 @@
+#!/bin/env raku
+
+class Point {
+ has Int $.x;
+ has Int $.y;
+}
+
+sub area(Point:D $a, Point:D $b, Point:D $c) {
+ abs(($a.x * ($b.y - $c.y) + $b.x * ($c.y - $a.y) + $c.x * ($a.y - $b.y)) / 2);
+}
+
+sub USAGE() {
+ say "Usage: {$*PROGRAM-NAME} <6 integers>";
+}
+
+multi MAIN(*@A where @A.elems == 6 && @A.all ~~ Int) {
+ my Point $a .= new(x => @A[0], y => @A[1]);
+ my Point $b .= new(x => @A[2], y => @A[3]);
+ my Point $c .= new(x => @A[4], y => @A[5]);
+ my Point $o .= new(x => 0, y => 0);
+
+ say +(area($a, $b, $c) == area($a, $b, $o) + area($b, $c, $o) + area($c, $a, $o));
+}