From ae6ac9d417b38f8bcc9cf150e7a5eb2ec7ce43f0 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 27 Jul 2020 09:00:39 +0100 Subject: Peaks challenge --- challenge-071/simon-proctor/raku/ch-1.raku | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 challenge-071/simon-proctor/raku/ch-1.raku diff --git a/challenge-071/simon-proctor/raku/ch-1.raku b/challenge-071/simon-proctor/raku/ch-1.raku new file mode 100644 index 0000000000..af23f3ea3b --- /dev/null +++ b/challenge-071/simon-proctor/raku/ch-1.raku @@ -0,0 +1,14 @@ +#!/usr/bin/env raku + +use v6; + +# Generate a list of random numbers then find the picks +sub MAIN( + UInt $N where 1 < * <= 50 #= Size of random number list +) { + my @list = (1..50).pick($N); + my @peaks = ( 0, |@list, |0 ).rotor(3 => -2).grep( { $_[0] < $_[1] > $_[2] } ).map( { $_[1] } ); + say "List : {@list.join(',')}"; + say "Peaks : {@peaks.join(',')}"; + +} -- cgit From 81ee5e376bb770ba1c77d91650f14a11fe8b6933 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 27 Jul 2020 10:23:56 +0100 Subject: Linked Lists again --- challenge-071/simon-proctor/raku/ch-2.raku | 95 ++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 challenge-071/simon-proctor/raku/ch-2.raku diff --git a/challenge-071/simon-proctor/raku/ch-2.raku b/challenge-071/simon-proctor/raku/ch-2.raku new file mode 100644 index 0000000000..ddb1154499 --- /dev/null +++ b/challenge-071/simon-proctor/raku/ch-2.raku @@ -0,0 +1,95 @@ +#!/usr/bin/env raku + +# Linked List Implementation modified from Challenge 68 +class ListNode { + has $.value; + has ListNode $!next; + + method is-last() { + ! $!next.defined; + } + + method next() { + $!next; + } + + method add-next( ListNode $next ) { + $!next = $next; + } + + method remove-next() { + if ( $!next.is-last ) { + $!next = ListNode; + } else { + $!next = $!next.next; + } + } + + method move-node( ListNode $start, ListNode $node ) { + my $prev = $start; + my $next = $!next; + my $node-next = $node.next; + while $prev.next !~~ $node { $prev = $prev.next } + $!next = $node; + $node.add-next( $next ); + $prev.add-next( $node-next ); + } + + method length( $count = 1 ) { + if self.is-last { + return $count; + } else { + return $!next.length( $count+1 ); + } + } + + method gist() { + if self.is-last { + "$.value"; + } else { + "{$.value} -> {$!next.gist}"; + } + } + + method from-iterator( ListNode:U : @values ) { + my ( $start-node, $node ); + + for @values -> $value { + if ! $start-node.defined { + $start-node = ListNode.new( :$value ); + $node = $start-node; + } else { + my $next-node = ListNode.new( :$value ); + $node.add-next( $next-node ); + $node = $next-node; + } + } + return $start-node; + } + + method last-node() { + my $node = self; + while ! $node.is-last { $node = $node.next(); } + return $node; + } +} + +sub MAIN ( + UInt $N, #= Number from the end to remove if N is greated than list length remove the first item + *@list, #= List of values +) { + my $linked = ListNode.from-iterator(@list); + say "Before: {$linked.gist}"; + if ( $N >= $linked.length ) { + $linked = $linked.next; + } else { + my $count = $linked.length - 1; + my $cur = $linked; + while ( $count > $N ) { + $count--; + $cur = $cur.next; + } + $cur.remove-next; + } + say "After: {$linked.gist}"; +} -- cgit From 0a597f43233d888b36666d5b1c94960eaf62a9f1 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 27 Jul 2020 10:26:02 +0100 Subject: Check N is greated than 0 --- challenge-071/simon-proctor/raku/ch-2.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-071/simon-proctor/raku/ch-2.raku b/challenge-071/simon-proctor/raku/ch-2.raku index ddb1154499..6ce64bb2d5 100644 --- a/challenge-071/simon-proctor/raku/ch-2.raku +++ b/challenge-071/simon-proctor/raku/ch-2.raku @@ -75,7 +75,7 @@ class ListNode { } sub MAIN ( - UInt $N, #= Number from the end to remove if N is greated than list length remove the first item + UInt $N where * > 0, #= Number from the end to remove if N is greated than list length remove the first item *@list, #= List of values ) { my $linked = ListNode.from-iterator(@list); -- cgit