diff options
Diffstat (limited to 'challenge-059')
| -rw-r--r-- | challenge-059/kevin-colyer/raku/ch-1.p6 | 193 | ||||
| -rw-r--r-- | challenge-059/kevin-colyer/raku/ch-2.p6 | 59 | ||||
| l--------- | challenge-059/kevincolyer | 1 |
3 files changed, 253 insertions, 0 deletions
diff --git a/challenge-059/kevin-colyer/raku/ch-1.p6 b/challenge-059/kevin-colyer/raku/ch-1.p6 new file mode 100644 index 0000000000..525a17b692 --- /dev/null +++ b/challenge-059/kevin-colyer/raku/ch-1.p6 @@ -0,0 +1,193 @@ +#!perl6 +# Task 1 Challenge 059 Solution by kevincolyer + +use Test; +#TASK #1 › Linked List +#Reviewed by Ryan Thompson +#You are given a linked list and a value k. +# Write a script to partition the linked list such that all nodes less than k +# come before nodes greater than or equal to k. +# Make sure you preserve the original relative order of the nodes in each of the two partitions. +# +#For example: +# +#Linked List: 1 → 4 → 3 → 2 → 5 → 2 +# +#k = 3 +# +#Expected Output: 1 → 2 → 2 → 4 → 3 → 5. +# + +class DLLNode { + has Int $.value is rw; + has DLLNode $.next is rw; + has DLLNode $.prev is rw; + method display { + return " -> " ~ $.value; + } +} + +class LinkedList { + has DLLNode $.head is rw; + has DLLNode $.tail is rw; + has Int $!items=0; + + method is-tail(DLLNode $n) { + return $n===$.tail; + } + + method is-head(DLLNode $n) { + return $n===$.head; + } + + method display { + return "Empty List" if $!items==0; + my $n = $.head; + my $s="(Items $!items)|-> " ~ $n.value; + while not self.is-tail($n) { + $n=$n.next; + $s~=$n.display; + } + return $s; + } + + method add-to-tail($v) { + my $n= DLLNode.new(value=>$v); + $!items++; + if $!items==1 { + self.head=$n; + self.tail=$n; + return; + } + $n.prev=self.tail; + $n.prev.next=$n; + self.tail=$n; + } + + method add-to-head($v) { + my $n=DLLNode.new(value=>$v); + $!items++; + if $!items==0 { + self.head=$n; + self.tail=$n; + return; + } + self.head.prev=$n; + $n.next=self.head; + self.head=$n; + } + + method add-after(DLLNode $n, Int $v) { + if self.is-tail($n) { self.add-to-tail($v); return }; + my $i = DLLNode.new(value => $v); + $i.prev=$n; + $i.next=$n.next; + $i.next.prev=$i; + $n.next=$i; + $!items++; + } + + method remove-node($n) { + return if $!items==0; + $!items--; + if $!items==0 { + self.head=Nil; + self.tail=Nil; + $n.prev=Nil; + $n.next=Nil; + return $n; + } + if self.is-head($n) { + self.head=$n.next; + self.head.prev=Nil; + $n.next=Nil; + return $n; + } + if self.is-tail($n) { + self.tail=$n.prev; + self.tail.next=Nil; + $n.prev=Nil; + return $n; + } + $n.prev.next=$n.next; + $n.next.prev=$n.prev; + $n.next=Nil; + $n.prev=Nil; + return $n; + } + + method partioned-sort(Int $k) { + # search for k + # from k onwards + # skip if >= + # otherwise search from root, place asap + my DLLNode $n = self.head; + loop { + last if $n.value==$k; + die "[$k] not found in list" if self.is-tail($n); + $n=$n.next; + } + die "[$k] is end of list already" if self.is-tail($n); + + $n.=next; + my $next=$n; + loop { + $next=$n.next; + if $n.value <= $k { + $n = self.remove-node($n); + my $i=self.head; + $i.=next while $i.value < $n.value; + self.add-after($i.prev,$n.value); + } + last unless $next.defined; + $n=$next; + } + } +} + +my $ll = LinkedList.new; +is $ll.display,"Empty List","test empty"; +$ll.add-to-tail(1); +is $ll.display,"(Items 1)|-> 1","test add to tail"; +$ll.add-to-tail(2); +is $ll.display,"(Items 2)|-> 1 -> 2","test add to tail"; +$ll.add-to-tail(3); +is $ll.display,"(Items 3)|-> 1 -> 2 -> 3","test add to tail"; +$ll.add-to-head(0); +is $ll.display,"(Items 4)|-> 0 -> 1 -> 2 -> 3","test add to head"; + +my $h = $ll.head; +my $t = $ll.tail; +$ll.remove-node($t); +is $ll.display,"(Items 3)|-> 0 -> 1 -> 2","test tail remove"; +$ll.remove-node($h); +is $ll.display,"(Items 2)|-> 1 -> 2","test head remove"; + +$h = $ll.head; +$t = $ll.tail; +$ll.remove-node($t); +is $ll.display,"(Items 1)|-> 1","test tail remove"; +$ll.remove-node($h); +is $ll.display,"Empty List","test head remove"; + +$ll.add-to-tail(1); +$ll.add-to-tail(4); +$h=$ll.head; +$t=$ll.tail; +$ll.add-after($h,0); +is $ll.display,"(Items 3)|-> 1 -> 0 -> 4","test add after"; +$ll.add-after($t,0); +is $ll.display,"(Items 4)|-> 1 -> 0 -> 4 -> 0","test add after"; +$ll.remove-node($ll.tail); +$ll.remove-node($ll.tail); +$ll.remove-node($ll.tail); +$ll.remove-node($ll.tail); + +$ll.add-to-tail($_) for 1, 4, 3, 2, 5, 2; +is $ll.display,"(Items 6)|-> 1 -> 4 -> 3 -> 2 -> 5 -> 2","before partitioned sort"; + +diag "partitioning by k=3"; +$ll.partioned-sort(3); +is $ll.display,"(Items 6)|-> 1 -> 2 -> 2 -> 4 -> 3 -> 5","after partitioned sort"; + +done-testing; diff --git a/challenge-059/kevin-colyer/raku/ch-2.p6 b/challenge-059/kevin-colyer/raku/ch-2.p6 new file mode 100644 index 0000000000..714aae9d07 --- /dev/null +++ b/challenge-059/kevin-colyer/raku/ch-2.p6 @@ -0,0 +1,59 @@ +#!perl6 +# Task 2 Challenge 059 Solution by kevincolyer + +#TASK #2 › Bit Sum +#Reviewed by Ryan Thompson +#Helper Function +#For this task, you will most likely need a function f(a,b) which returns the count of different bits of binary representation of a and b. +# +#For example, f(1,3) = 1, since: +# +#Binary representation of 1 = 01 +# +#Binary representation of 3 = 11 +# +#There is only 1 different bit. Therefore the subroutine should return 1. Note that if one number is longer than the other in binary, the most significant bits of the smaller number are padded (i.e., they are assumed to be zeroes). +# +#Script Output +#You script should accept n positive numbers. Your script should sum the result of f(a,b) for every pair of numbers given: +# +#For example, given 2, 3, 4, the output would be 6, since f(2,3) + f(2,4) + f(3,4) = 1 + 2 + 3 = 6 + +# F XORs bits then counts them by mask of bit one, shifts left, ends when empty + +use Test; + +subset PosInt of Int where * >=0 ; + +sub f(PosInt $a, PosInt $b) { + my Int $count=0; + my Int $bits = $a +^ $b; + while $bits>0 { + $count+= $bits +& 1; + $bits=$bits +> 1; + } + return $count; +} + +multi MAIN('test') { + is f(1,3),1,"example"; + is f(2,3),1,"example 2"; + is f(2,4),2,"example 2"; + is f(4,3),3,"example 2"; + is f(2,3)+f(2,4)+f(3,4),6,"example 2"; +} + +#|Sum the different bits of pairs of positive ints... +multi MAIN(+@n) { + die "Need pairs of numbers, got {@n.elems}" unless @n %% 2 && @n.elems > 0; + say [+] gather for @n -> $a,$b { take f($a,$b) }; +} + +# http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel +# faster solution +# for 32 bits +# c = (v & 0x55555555) + ((v >> 1) & 0x55555555); +# c = (c & 0x33333333) + ((c >> 2) & 0x33333333); +# c = (c & 0x0F0F0F0F) + ((c >> 4) & 0x0F0F0F0F); +# c = (c & 0x00FF00FF) + ((c >> 8) & 0x00FF00FF); +# c = (c & 0x0000FFFF) + ((c >> 16)& 0x0000FFFF); diff --git a/challenge-059/kevincolyer b/challenge-059/kevincolyer new file mode 120000 index 0000000000..8fc47c15c2 --- /dev/null +++ b/challenge-059/kevincolyer @@ -0,0 +1 @@ +kevin-colyer
\ No newline at end of file |
