diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-07-27 23:18:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-27 23:18:22 +0100 |
| commit | 4968299215d8c6f622db49cae4d3d9d6d235c4aa (patch) | |
| tree | 431cab7dacea49e0e9c426b32ab03da29650b35b | |
| parent | 6b0fef6d35e61345ad5fd3f2ab5847c0afafc5ff (diff) | |
| parent | 0a597f43233d888b36666d5b1c94960eaf62a9f1 (diff) | |
| download | perlweeklychallenge-club-4968299215d8c6f622db49cae4d3d9d6d235c4aa.tar.gz perlweeklychallenge-club-4968299215d8c6f622db49cae4d3d9d6d235c4aa.tar.bz2 perlweeklychallenge-club-4968299215d8c6f622db49cae4d3d9d6d235c4aa.zip | |
Merge pull request #1987 from Scimon/master
Peaks challenge
| -rw-r--r-- | challenge-071/simon-proctor/raku/ch-1.raku | 14 | ||||
| -rw-r--r-- | challenge-071/simon-proctor/raku/ch-2.raku | 95 |
2 files changed, 109 insertions, 0 deletions
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(',')}"; + +} 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..6ce64bb2d5 --- /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 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); + 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}"; +} |
