aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchirvasitua <stuart-little@users.noreply.github.com>2021-01-04 01:25:09 -0500
committerchirvasitua <stuart-little@users.noreply.github.com>2021-01-04 01:25:09 -0500
commitc0fe48da889e020aada5fafd60ad43ca4a26f7bb (patch)
treeee7b6fc1a469b33803357fdb39ca6966190bed74
parent76c85834bf5cacaa85a9c3096a50a12855386770 (diff)
downloadperlweeklychallenge-club-c0fe48da889e020aada5fafd60ad43ca4a26f7bb.tar.gz
perlweeklychallenge-club-c0fe48da889e020aada5fafd60ad43ca4a26f7bb.tar.bz2
perlweeklychallenge-club-c0fe48da889e020aada5fafd60ad43ca4a26f7bb.zip
1st commit on 094_raku
-rwxr-xr-xchallenge-094/stuart-little/raku/ch-1.p66
-rwxr-xr-xchallenge-094/stuart-little/raku/ch-2.p6110
2 files changed, 116 insertions, 0 deletions
diff --git a/challenge-094/stuart-little/raku/ch-1.p6 b/challenge-094/stuart-little/raku/ch-1.p6
new file mode 100755
index 0000000000..44a0ba2ee9
--- /dev/null
+++ b/challenge-094/stuart-little/raku/ch-1.p6
@@ -0,0 +1,6 @@
+#!/usr/bin/env perl6
+use v6;
+
+# run as <script> <space-separated words>
+
+for @*ARGS.classify(*.comb.Bag).values {.say}
diff --git a/challenge-094/stuart-little/raku/ch-2.p6 b/challenge-094/stuart-little/raku/ch-2.p6
new file mode 100755
index 0000000000..46c9e8768d
--- /dev/null
+++ b/challenge-094/stuart-little/raku/ch-2.p6
@@ -0,0 +1,110 @@
+#!/usr/bin/env perl6
+use v6;
+
+class Node {
+ has Real $.val;
+ has Node $.nxt is rw;
+}
+
+class Sll {
+ has Node $.head is rw;
+
+ method show() {
+ my $node = self.head;
+ (! $node) && return;
+ while ($node.nxt) {
+ print $node.val, " -> ";
+ $node=$node.nxt;
+ }
+ say $node.val;
+ }
+
+ method len() {
+ my $node = self.head;
+ my $len=0;
+ while ($node) {
+ $len++;
+ $node=$node.nxt;
+ }
+ return $len;
+ }
+
+ multi method insert(Real $val) {
+ my $node=self.head;
+ (! $node) && do {
+ self.head=Node.new(val => $val);
+ return;
+ }
+ while ($node.nxt) {
+ $node=$node.nxt;
+ }
+ $node.nxt=Node.new(val => $val);
+ }
+
+ multi method insert(0, Real $val) {
+ my $node=Node.new(val => $val, nxt => self.head);
+ self.head=$node;
+ }
+
+ multi method insert(Int $idx where * > 0, Real $val) {
+ given self.len {
+ when 0 { self.insert(0,$val) }
+ when 1..$idx { self.insert($val) }
+ default {
+ my $cur=self.head;
+ my $node=Node.new(val => $val);
+ for (0..^($idx-1)) {
+ $cur=$cur.nxt;
+ }
+ $node.nxt=$cur.nxt;
+ $cur.nxt=$node;
+ }
+ }
+ }
+
+ method del(Int $idx where * >= 0) {
+ given $idx {
+ when self.len..* { warn "Index out of range" }
+ when 0 { self.head=self.head.nxt }
+ default {
+ my $cur=self.head;
+ for (^($idx-1)) {
+ $cur=$cur.nxt;
+ }
+ $cur.nxt=$cur.nxt.nxt;
+ }
+ }
+ }
+}
+
+my $ll=Sll.new();
+for ( @*ARGS.grep({ $_ ne "."}).map(*.Real) ) {
+ $ll.insert($_);
+}
+
+print "List form of the tree: ";
+$ll.show();
+
+=finish
+
+This is *very* similar to
+
+https://perlweeklychallenge.org/blog/perl-weekly-challenge-093/#TASK2
+
+so I am reusing code.
+
+run as <script> <tree in preorder form with '.' for empty nodes, entered as space-separated values>
+
+ref: https://stackoverflow.com/a/2676849/11064961
+
+e.g. 1 2 4 . . 5 6 . . 7 . . 3 . . represents the tree
+
+ 1
+ / \
+ 2 3
+ / \
+ 4 5
+ / \
+ 6 7
+
+given as an example in the problem formulation at https://perlweeklychallenge.org/blog/perl-weekly-challenge-094/