From 94790f02a8df556d6ebf09d9225e44f2505039d7 Mon Sep 17 00:00:00 2001 From: Noud Aldenhoven Date: Sat, 11 Jul 2020 15:56:13 +0200 Subject: Solution to challenge 68 task 1 and 2 in Raku by Noud --- challenge-068/noud/raku/ch-1.p6 | 59 ++++++++++++++++++++++++++++++++++ challenge-068/noud/raku/ch-2.p6 | 71 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 challenge-068/noud/raku/ch-1.p6 create mode 100644 challenge-068/noud/raku/ch-2.p6 diff --git a/challenge-068/noud/raku/ch-1.p6 b/challenge-068/noud/raku/ch-1.p6 new file mode 100644 index 0000000000..c75cd4f5f8 --- /dev/null +++ b/challenge-068/noud/raku/ch-1.p6 @@ -0,0 +1,59 @@ +# You are given a matrix of size M x N having only 0s and 1s. +# +# Write a script to set the entire row and column to 0 if an element is 0. +# Example 1 +# +# Input: [1, 0, 1] +# [1, 1, 1] +# [1, 1, 1] +# +# Output: [0, 0, 0] +# [1, 0, 1] +# [1, 0, 1] +# +# Example 2 +# +# Input: [1, 0, 1] +# [1, 1, 1] +# [1, 0, 1] +# +# Output: [0, 0, 0] +# [1, 0, 1] +# [0, 0, 0] +# + +my @matrix1 = [ + [1, 0, 1], + [1, 1, 1], + [1, 1, 1] +]; + +my @matrix2 = [ + [1, 0, 1], + [1, 1, 1], + [1, 0, 1] +]; + +sub zero-matrix(@matrix, $m, $n) { + my @zero-entries = []; + for ^$m X ^$n -> ($i, $j) { + if (@matrix[$i][$j] == 0) { + @zero-entries.push([$i, $j]); + } + } + + for @zero-entries -> ($i, $j) { + for ^$m -> $k { + @matrix[$k][$j] = 0; + } + for ^$n -> $l { + @matrix[$i][$l] = 0; + } + } + + return @matrix; +} + +.say for zero-matrix(@matrix1, 3, 3); +say ''; +.say for zero-matrix(@matrix2, 3, 3); diff --git a/challenge-068/noud/raku/ch-2.p6 b/challenge-068/noud/raku/ch-2.p6 new file mode 100644 index 0000000000..7258617954 --- /dev/null +++ b/challenge-068/noud/raku/ch-2.p6 @@ -0,0 +1,71 @@ +# You are given a singly linked list $L as below: +# +# L0 → L1 → … → Ln-1 → Ln +# +# Write a script to reorder list as below: +# +# L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → +# +# You are ONLY allowed to do this in-place without altering the nodes’ values. +# Example +# +# Input: 1 → 2 → 3 → 4 +# Output: 1 → 4 → 2 → 3 +# + +class Node { + has Int $.value is rw; + has Node $.next is rw; +} + +class LinkedList { + has Node $.head is rw; + + method create-list(@values) { + my $cur-node = $!head; + + for @values -> $v { + my $new-node = Node.new(value => $v); + if ($cur-node) { + $cur-node.next = $new-node; + } else { + $!head = $new-node; + } + $cur-node = $new-node; + } + } + + method print-list() { + my $cur-node = $!head; + my @values = []; + while ($cur-node) { + @values.push($cur-node.value); + $cur-node = $cur-node.next; + } + say @values.join(' -> '); + } + + method reorder-list() { + my $cur-node = $!head; + while ($cur-node.next and $cur-node.next.next) { + my $one-but-last-node = $cur-node; + my $last-node = $cur-node.next; + + while ($last-node.next) { + $one-but-last-node = $last-node; + $last-node = $last-node.next; + } + + $last-node.next = $cur-node.next; + $cur-node.next = $last-node; + $one-but-last-node.next = Nil; + $cur-node = $last-node.next; + } + } +} + +my $linked-list = LinkedList.new(); +$linked-list.create-list([1, 2, 3, 4, 5, 6, 7, 8]); +$linked-list.print-list(); +$linked-list.reorder-list(); +$linked-list.print-list(); -- cgit