aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-017/stuart-little/README1
-rwxr-xr-xchallenge-017/stuart-little/raku/ch-1.p613
-rwxr-xr-xchallenge-017/stuart-little/raku/ch-2.p631
-rw-r--r--challenge-018/stuart-little/README1
-rwxr-xr-xchallenge-018/stuart-little/raku/ch-1.p66
-rwxr-xr-xchallenge-018/stuart-little/raku/ch-2.p641
-rw-r--r--challenge-019/stuart-little/README1
-rwxr-xr-xchallenge-019/stuart-little/raku/ch-1.p69
-rwxr-xr-xchallenge-019/stuart-little/raku/ch-2.p621
-rw-r--r--challenge-020/stuart-little/README1
-rwxr-xr-xchallenge-020/stuart-little/raku/ch-1.p66
-rwxr-xr-xchallenge-020/stuart-little/raku/ch-2.p68
12 files changed, 139 insertions, 0 deletions
diff --git a/challenge-017/stuart-little/README b/challenge-017/stuart-little/README
new file mode 100644
index 0000000000..78439907de
--- /dev/null
+++ b/challenge-017/stuart-little/README
@@ -0,0 +1 @@
+Solutions by Stuart Little
diff --git a/challenge-017/stuart-little/raku/ch-1.p6 b/challenge-017/stuart-little/raku/ch-1.p6
new file mode 100755
index 0000000000..1519a9e698
--- /dev/null
+++ b/challenge-017/stuart-little/raku/ch-1.p6
@@ -0,0 +1,13 @@
+#!/usr/bin/env perl6
+use v6;
+
+multi sub A(0,Int $n) { $n + 1 }
+multi sub A(1,Int $n) { $n + 2 }
+multi sub A(2,Int $n) { 2*$n + 3 }
+multi sub A(3,Int $n) { 2 ** ($n + 3) -3 }
+multi sub A(Int $m,0) { A($m-1,1) }
+multi sub A(Int $m,Int $n) { A($m-1,A($m,$n-1)) }
+
+say A(|@*ARGS[0,1].map(*.Int))
+
+# run as <script> <m n> to compute A(m,n)
diff --git a/challenge-017/stuart-little/raku/ch-2.p6 b/challenge-017/stuart-little/raku/ch-2.p6
new file mode 100755
index 0000000000..b41f2a9a6c
--- /dev/null
+++ b/challenge-017/stuart-little/raku/ch-2.p6
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl6
+use v6;
+
+grammar URL {
+ regex TOP { <scheme>':'(\/\/<authority>)?<path>(\?<query>)?('#'<fragment>)? }
+ regex scheme { .+? }
+
+ regex authority { (<userinfo>'@')?<host>(':'<port>)? }
+ regex userinfo { .+ }
+ regex host { <-[:]>+ }
+ regex port { \d+ }
+
+ regex path { <-[?]>* }
+ regex query { .+? }
+ regex fragment { .+ }
+}
+
+URL.parse(@*ARGS[0]);
+my $res = qq:to/END/;
+
+scheme: $/.<scheme>
+userinfo: $/.[0].<authority>.[0].<userinfo>
+host: $/.[0].<authority>.<host>
+port: $/.[0].<authority>.[1].<port>
+path: $/.<path>
+query: $/.[1].<query>
+fragment: $/.[2].<fragment>
+END
+say $res;
+
+# run as <script> <string to validate as URL>
diff --git a/challenge-018/stuart-little/README b/challenge-018/stuart-little/README
new file mode 100644
index 0000000000..78439907de
--- /dev/null
+++ b/challenge-018/stuart-little/README
@@ -0,0 +1 @@
+Solutions by Stuart Little
diff --git a/challenge-018/stuart-little/raku/ch-1.p6 b/challenge-018/stuart-little/raku/ch-1.p6
new file mode 100755
index 0000000000..b53f9d8f9e
--- /dev/null
+++ b/challenge-018/stuart-little/raku/ch-1.p6
@@ -0,0 +1,6 @@
+#!/usr/bin/env perl6
+use v6;
+
+say (0..( my $minword=@*ARGS.min(*.chars); $minword.chars ).[1]).combinations(2).map({ $minword.substr($_[0]..^$_[1]) }).grep({ @*ARGS.all.contains($_) }).max(*.chars)
+
+# run as <script> <space-separated strings, each enclosed in quotes if it contains spaces>
diff --git a/challenge-018/stuart-little/raku/ch-2.p6 b/challenge-018/stuart-little/raku/ch-2.p6
new file mode 100755
index 0000000000..f7cb9541c3
--- /dev/null
+++ b/challenge-018/stuart-little/raku/ch-2.p6
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl6
+use v6;
+
+class Item {
+ has Int $.val;
+ has Int $.priority;
+}
+
+class PriorityQueue is Array {
+
+ method is_empty {
+ return self.elems==0
+ }
+
+ method insert_with_priority($val,$priority) {
+ my $item=Item.new(val=>$val, priority=>$priority);
+ my @rhs=self.grep({ $_.priority >= $priority });
+ @rhs.unshift: $item;
+ self=(|self.grep({ $_.priority < $priority }), |@rhs)
+ }
+
+ method pull_highest_priority_element {
+ return self.pop.val
+ }
+
+
+}
+
+my $prq = PriorityQueue.new;
+
+say "Is the queue empty initially? ", $prq.is_empty;
+
+for ( 1=>2, 3=>2, -1=>0, 10=>5 ) {
+ $prq.insert_with_priority($_.key,$_.value);
+}
+
+say "Queue after adding some items: ", $prq;
+say "Is the queue empty now? ", $prq.is_empty;
+say "Highest-priority item: ", $prq.pull_highest_priority_element;
+
+# run as <script>
diff --git a/challenge-019/stuart-little/README b/challenge-019/stuart-little/README
new file mode 100644
index 0000000000..78439907de
--- /dev/null
+++ b/challenge-019/stuart-little/README
@@ -0,0 +1 @@
+Solutions by Stuart Little
diff --git a/challenge-019/stuart-little/raku/ch-1.p6 b/challenge-019/stuart-little/raku/ch-1.p6
new file mode 100755
index 0000000000..35a5aee266
--- /dev/null
+++ b/challenge-019/stuart-little/raku/ch-1.p6
@@ -0,0 +1,9 @@
+#!/usr/bin/env perl6
+use v6;
+
+for ((1900..2019) X (1..12))
+.grep({ my $date=Date.new($_[0],$_[1],1);
+ my @range=($date..$date.last-date-in-month);
+ (5,6,7).map(-> $day { @range.grep({ $_.day-of-week==$day }).elems }).all == 5 }) {.say}
+
+# run as <script>
diff --git a/challenge-019/stuart-little/raku/ch-2.p6 b/challenge-019/stuart-little/raku/ch-2.p6
new file mode 100755
index 0000000000..64f0cce357
--- /dev/null
+++ b/challenge-019/stuart-little/raku/ch-2.p6
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl6
+use v6;
+
+my %*SUB-MAIN-OPTS=:named-anywhere,;
+sub MAIN(
+ Str :f(:$file),
+ Int :w(:$width) =80,
+ ) {
+ my $text=(($file) ?? ($file.IO.slurp) !! $=finish).split(/\s+/).join(" ");
+
+ for wrp($text,$width) {.say}
+}
+
+sub wrp($text,$width) {
+ $text.chars <= $width && return ($text,);
+ my $idx=$text.substr(0,$width).rindex(' ') || $width-1;
+ return ($text.substr(0,$idx),|wrp($text.substr($idx+1), $width))
+}
+
+=finish
+On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains.
diff --git a/challenge-020/stuart-little/README b/challenge-020/stuart-little/README
new file mode 100644
index 0000000000..78439907de
--- /dev/null
+++ b/challenge-020/stuart-little/README
@@ -0,0 +1 @@
+Solutions by Stuart Little
diff --git a/challenge-020/stuart-little/raku/ch-1.p6 b/challenge-020/stuart-little/raku/ch-1.p6
new file mode 100755
index 0000000000..cf96633111
--- /dev/null
+++ b/challenge-020/stuart-little/raku/ch-1.p6
@@ -0,0 +1,6 @@
+#!/usr/bin/env perl6
+use v6;
+
+@*ARGS[0] ~~ m:g/(.)$0*/; say $/.map(*.Str)
+
+# run as <script> <string>
diff --git a/challenge-020/stuart-little/raku/ch-2.p6 b/challenge-020/stuart-little/raku/ch-2.p6
new file mode 100755
index 0000000000..15e1b49127
--- /dev/null
+++ b/challenge-020/stuart-little/raku/ch-2.p6
@@ -0,0 +1,8 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub divisors($n) { (1..^$n).grep($n %% *) }
+
+say (2..*).map({ $_, $_.&divisors.sum }).grep({ $_[0] < $_[1] && $_[1].&divisors.sum==$_[0] }).[(@*ARGS) ?? (0..@*ARGS[0].Int) !! 0]
+
+# run as <script>