aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2020-07-27 11:46:43 +0200
committerLuca Ferrari <fluca1978@gmail.com>2020-07-27 11:46:43 +0200
commit3b70cc0bc2867dbfe9e2d7606f81794ee7e6e57c (patch)
treec1b8948abadf89ecf51db04e7902c4a12226003f
parent2185e9969ea009568273bfa2a2e3603b06387631 (diff)
downloadperlweeklychallenge-club-3b70cc0bc2867dbfe9e2d7606f81794ee7e6e57c.tar.gz
perlweeklychallenge-club-3b70cc0bc2867dbfe9e2d7606f81794ee7e6e57c.tar.bz2
perlweeklychallenge-club-3b70cc0bc2867dbfe9e2d7606f81794ee7e6e57c.zip
Task2 done.
-rw-r--r--challenge-071/luca-ferrari/raku/ch-2.p681
1 files changed, 81 insertions, 0 deletions
diff --git a/challenge-071/luca-ferrari/raku/ch-2.p6 b/challenge-071/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..43e64e31ed
--- /dev/null
+++ b/challenge-071/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,81 @@
+#!raku
+
+
+class Node {
+ has $.value;
+ has $.next is rw;
+
+
+ method Str() {
+ my $current = self;
+ my $string;
+ while ( $current ) {
+ $string ~= "{ $current.value }";
+ $string ~= " -> " if $current.next;
+ $current = $current.next;
+ }
+
+ $string;
+ }
+
+
+ method size() {
+ my $size-of-the-list = 0;
+ my $current = self;
+ while ( $current ) {
+ $size-of-the-list++;
+ $current = $current.next;
+ }
+
+ $size-of-the-list;
+ }
+}
+
+sub MAIN( Int $N where { $N > 0 }, *@list ) {
+ say @list;
+
+ # build the list backward and keep the root
+ # at the list
+ my $root = Nil;
+ my $current = Nil;
+ loop ( my $i = @list.elems - 1; $i >= 0; $i-- ) {
+ $root = Node.new( value => @list[ $i ],
+ next => $root );
+ }
+
+ # compute the size of the list
+ my $size-of-the-list = $root.size;
+ "Size of the list is $size-of-the-list".say;
+ $root.put;
+
+
+
+
+
+ my $index = 1;
+ my $index-to-remove = $size-of-the-list - $N + 1;
+
+
+ # particular case: remove the root
+ $root = $root.next if ( $index == $index-to-remove || $N > $size-of-the-list );
+
+ # remove a specific element, but only if this has not been already done
+ # by removing the root element and thus changing the size of the list
+ if ( $root.size == $size-of-the-list ) {
+ $current = $root;
+ while ( $current ) {
+ if ( ( $index + 1 ) == ( $size-of-the-list - $N + 1 ) ) {
+ $current.next = $current.next.next;
+ last;
+ }
+
+ $index++;
+ $current = $current.next;
+ }
+ }
+
+
+ # print the modified list
+ $root.put;
+
+}