aboutsummaryrefslogtreecommitdiff
path: root/challenge-059
diff options
context:
space:
mode:
authorkevincolyer <>2020-05-09 17:06:20 +0100
committerkevincolyer <>2020-05-09 17:06:20 +0100
commit97eed38d949d36fe7277adcbaddc40e2dc205f56 (patch)
tree61938f036c2b4d96a8f1ea3c7d8fa241a4111741 /challenge-059
parent4c48d691c43e6ee2af4f89c690fb228d5ecd8947 (diff)
downloadperlweeklychallenge-club-97eed38d949d36fe7277adcbaddc40e2dc205f56.tar.gz
perlweeklychallenge-club-97eed38d949d36fe7277adcbaddc40e2dc205f56.tar.bz2
perlweeklychallenge-club-97eed38d949d36fe7277adcbaddc40e2dc205f56.zip
ch 2 done. Refactoring ch 1
Diffstat (limited to 'challenge-059')
-rw-r--r--challenge-059/kevin-colyer/raku/ch-1.p6182
1 files changed, 105 insertions, 77 deletions
diff --git a/challenge-059/kevin-colyer/raku/ch-1.p6 b/challenge-059/kevin-colyer/raku/ch-1.p6
index 970770e46d..c13cef6760 100644
--- a/challenge-059/kevin-colyer/raku/ch-1.p6
+++ b/challenge-059/kevin-colyer/raku/ch-1.p6
@@ -17,90 +17,118 @@ use Test;
#Expected Output: 1 → 2 → 2 → 4 → 3 → 5.
#
-
+class SLLNode {
+ has Int $.value is rw;
+ has SLLNode $.next is rw;
+ method display {
+ return " -> " ~ $!value;
+ }
+}
class LinkedList {
- has Int $.value;
- has LinkedList $.next is rw;
- # has LinkedList $.prev is rw;
- method is-tail {
- return $!next.defined ?? False !! True;
+ has SLLNODE $.head is rw;
+ has SLLNODE $.tail is rw;
+ has Int $.items;
+
+ method is-tail(SLLNode $n) {
+ return $n==$tail;
}
+
method display {
- my $s = " -> " ~ $!value;
- if self.is-tail { return $s };
- return $s ~ self.next.display;
+ return "Empty List" if $!items=0;
+ my $n = $!head;
+ my $s="($!items)|-> " ~ $n.value;
+ while not self.is-tail($n) {
+ $n.=next;
+ $s~=" -> {$n.display}";
+ }
+ return $s;
+
+ }
+ method add-to-tail($v) {
+ my $n= SLLNode.new(value=>$v);
+ if $!items==0 {
+ self.head=$n;
+ self.tail=$n;
+ $!items++;
+ return;
+ }
+ self.tail.next=$n;
+ self.tail=$n;
+ $!items++;
}
}
-my $ExampleList = LinkedList.new(value => 1);
-
-is $ExampleList.value, 1, "value is 1";
-is $ExampleList.is-tail, True, "At end of list";
-$ExampleList.next = LinkedList.new(value => 4);
-is $ExampleList.is-tail, False, "Not at end of list";
-say $ExampleList.display;
+my $ll = LinkedList.new;
+say $ll.display;
-$ExampleList.next.next = LinkedList.new(value => 3);
-$ExampleList.next.next.next = LinkedList.new(value => 2);
-$ExampleList.next.next.next.next = LinkedList.new(value => 5);
-$ExampleList.next.next.next.next.next = LinkedList.new(value => 2);
-$ExampleList.next.next.next.next.next.next = LinkedList.new(value => 0);
-say $ExampleList.display;
-# search for k
-# from k onwards
-# skip if >=
-# otherwise search from root, place asap
-my $n = $ExampleList;
-my $k=3;
-my $kn= $ExampleList;
-
-loop {
- if $n.value==$k {
- $kn=$n;
- last;
- }
- die "[$k] not found in list" if $n.is-tail;
- $n=$n.next;
-}
-die "[$k] is end of list already" if $kn.is-tail;
-
-my $prev=$n;
-$n=$kn.next;
-say $n.display;
-
-loop {
- my $next=$n.next;
- say "n value is {$n.value}";
- if $n.value < $k {
- $prev.next=$next; # snip $n out of list
- say $ExampleList.display;
- my $search= $ExampleList; # search from beginning
- my $searchPrev= $ExampleList;
- if $search.value <= $n.value {
- $n.next=$ExampleList;
- $ExampleList=$n;
- $n=$next;
- next;
- }
- while $search.value < $n.value {
- say "searching for >={$n.value} got {$search.value}";
- $searchPrev=$search;
- $search=$searchPrev.next;
- }
- say "got {$search.value} inserting before {$searchPrev.value}";
- $searchPrev.next=$n; # reconnect into list
- $n.next=$search;
- say $ExampleList.display;
- # continue loop
- $n=$next;
- last if $prev.is-tail;
- next;
- }
- $prev=$n;
- $n=$next;
- last if $prev.is-tail;
-}
-say $ExampleList.display;
+# is $ExampleList.value, 1, "value is 1";
+# is $ExampleList.is-tail, True, "At end of list";
+# $ExampleList.next = LinkedList.new(value => 4);
+# is $ExampleList.is-tail, False, "Not at end of list";
+# say $ExampleList.display;
+#
+# $ExampleList.next.next = LinkedList.new(value => 3);
+# $ExampleList.next.next.next = LinkedList.new(value => 2);
+# $ExampleList.next.next.next.next = LinkedList.new(value => 5);
+# $ExampleList.next.next.next.next.next = LinkedList.new(value => 2);
+# $ExampleList.next.next.next.next.next.next = LinkedList.new(value => 0);
+# say $ExampleList.display;
+#
+# # search for k
+# # from k onwards
+# # skip if >=
+# # otherwise search from root, place asap
+#
+# my $n = $ExampleList;
+# my $k=3;
+# my $kn= $ExampleList;
+#
+# loop {
+# if $n.value==$k {
+# $kn=$n;
+# last;
+# }
+# die "[$k] not found in list" if $n.is-tail;
+# $n=$n.next;
+# }
+# die "[$k] is end of list already" if $kn.is-tail;
+#
+# my $prev=$n;
+# $n=$kn.next;
+# say $n.display;
+#
+# loop {
+# my $next=$n.next;
+# say "n value is {$n.value}";
+# if $n.value < $k {
+# $prev.next=$next; # snip $n out of list
+# say $ExampleList.display;
+# my $search= $ExampleList; # search from beginning
+# my $searchPrev= $ExampleList;
+# if $search.value <= $n.value {
+# $n.next=$ExampleList;
+# $ExampleList=$n;
+# $n=$next;
+# next;
+# }
+# while $search.value < $n.value {
+# say "searching for >={$n.value} got {$search.value}";
+# $searchPrev=$search;
+# $search=$searchPrev.next;
+# }
+# say "got {$search.value} inserting before {$searchPrev.value}";
+# $searchPrev.next=$n; # reconnect into list
+# $n.next=$search;
+# say $ExampleList.display;
+# # continue loop
+# $n=$next;
+# last if $prev.is-tail;
+# next;
+# }
+# $prev=$n;
+# $n=$next;
+# last if $prev.is-tail;
+# }