diff options
| author | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2020-07-31 14:42:56 +0200 |
|---|---|---|
| committer | Noud Aldenhoven <noud.aldenhoven@gmail.com> | 2020-07-31 14:42:56 +0200 |
| commit | b2c3eaca77d98bf90b8f6934a0062fba411def27 (patch) | |
| tree | 3c2bf847f5c99ba44325618c9ace4d579b5c6adb /challenge-071 | |
| parent | ab4664c5f733997e92e3357a515d003d6a5fd485 (diff) | |
| download | perlweeklychallenge-club-b2c3eaca77d98bf90b8f6934a0062fba411def27.tar.gz perlweeklychallenge-club-b2c3eaca77d98bf90b8f6934a0062fba411def27.tar.bz2 perlweeklychallenge-club-b2c3eaca77d98bf90b8f6934a0062fba411def27.zip | |
Solution to challenge 071 task 1 and 2 in Raku by Noud
Diffstat (limited to 'challenge-071')
| -rw-r--r-- | challenge-071/noud/raku/ch-1.p6 | 36 | ||||
| -rw-r--r-- | challenge-071/noud/raku/ch-2.p6 | 78 |
2 files changed, 114 insertions, 0 deletions
diff --git a/challenge-071/noud/raku/ch-1.p6 b/challenge-071/noud/raku/ch-1.p6 new file mode 100644 index 0000000000..d7fb10771d --- /dev/null +++ b/challenge-071/noud/raku/ch-1.p6 @@ -0,0 +1,36 @@ +# You are given positive integer $N (>1). +# +# Write a script to create an array of size $N with random unique elements +# between 1 and 50. +# +# In the end it should print peak elements in the array, if found. +# +# An array element is called peak if it is bigger than it’s neighbour. +# +# Example 1 +# Array: [ 18, 45, 38, 25, 10, 7, 21, 6, 28, 48 ] +# Peak: [ 48, 45, 21 ] +# Example 2 +# Array: [ 47, 11, 32, 8, 1, 9, 39, 14, 36, 23 ] +# Peak: [ 47, 32, 39, 36 ] + +sub random_array($N) { + return (1..50).pick(1..$N); +} + +sub get_peaks(@a) { + if (@a.elems == 1) { + return @a; + } + return [ + |([@a[0] if @a[0] > @a[1]]), + |([@a[$_] if ((@a[$_] > @a[$_ + 1]) and (@a[$_] > @a[$_ - 1])) for 1..(@a.elems - 2)]), + |([@a[*-1] if @a[*-1] > @a[*-2]]) + ]; +} + +my @a = random_array(10); +my @peaks = get_peaks(@a); + +@a.say; +@peaks.say; diff --git a/challenge-071/noud/raku/ch-2.p6 b/challenge-071/noud/raku/ch-2.p6 new file mode 100644 index 0000000000..644d8bd84e --- /dev/null +++ b/challenge-071/noud/raku/ch-2.p6 @@ -0,0 +1,78 @@ +# You are given a singly linked list and a positive integer $N (>0). +# +# Write a script to remove the $Nth node from the end of the linked list and +# print the linked list. +# +# If $N is greater than the size of the linked list then remove the first node +# of the list. +# +# NOTE: Please use pure linked list implementation. +# Example +# Given Linked List: 1 -> 2 -> 3 -> 4 -> 5 +# when $N = 1 +# Output: 1 -> 2 -> 3 -> 4 +# when $N = 2 +# Output: 1 -> 2 -> 3 -> 5 +# when $N = 3 +# Output: 1 -> 2 -> 4 -> 5 +# when $N = 4 +# Output: 1 -> 3 -> 4 -> 5 +# when $N = 5 +# Output: 2 -> 3 -> 4 -> 5 +# when $N = 6 +# Output: 2 -> 3 -> 4 -> 5 + +class Node { + has Int $.value is rw; + has Node $.next is rw; + + method remove($N) { + if ($N == 0) { + return self.next; + } + + my $cur = self; + my $index = 0; + while ($cur) { + if ($index + 1 == $N) { + $cur.next = $cur.next.next; + return self; + } + $index++; + $cur = $cur.next; + } + + return self.next; + } + + method display() { + my $cur = self; + my @values = []; + while ($cur) { + @values.push($cur.value); + $cur = $cur.next; + } + @values.join(" -> ").say; + } +} + +my $n = Node.new(); +my $cur = $n; +my @values = (0..10).reverse; +while (@values) { + $cur.value = @values.pop; + if (@values) { + $cur.next = Node.new(); + $cur = $cur.next; + } +} +$n.display(); + +$n = $n.remove(5); +$n.display; + +$n = $n.remove(11); +$n.display; + +$n = $n.remove(0); +$n.display; |
