diff options
Diffstat (limited to 'challenge-059')
| -rw-r--r-- | challenge-059/kevin-colyer/raku/ch-1.p6 | 106 | ||||
| -rw-r--r-- | challenge-059/kevin-colyer/raku/ch-2.p6 | 51 |
2 files changed, 157 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..970770e46d --- /dev/null +++ b/challenge-059/kevin-colyer/raku/ch-1.p6 @@ -0,0 +1,106 @@ +#!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 LinkedList { + has Int $.value; + has LinkedList $.next is rw; + # has LinkedList $.prev is rw; + method is-tail { + return $!next.defined ?? False !! True; + } + method display { + my $s = " -> " ~ $!value; + if self.is-tail { return $s }; + return $s ~ self.next.display; + } +} + +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; + +$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; 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..641a6c11f0 --- /dev/null +++ b/challenge-059/kevin-colyer/raku/ch-2.p6 @@ -0,0 +1,51 @@ +#!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 +sub f(Int $a, Int $b) { + my Int $count=0; + my Int $bits = $a +^ $b; + while $bits>0 { + $count+= $bits +& 1; + $bits=$bits +> 1; + } + return $count; +} + +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"; + +# accept n pos ints and add them... +sub MAIN(+@n) { + +} + +# 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); |
