aboutsummaryrefslogtreecommitdiff
path: root/challenge-071/ash
diff options
context:
space:
mode:
authorAndrew Shitov <andy@shitov.ru>2020-08-02 00:31:59 +0300
committerAndrew Shitov <andy@shitov.ru>2020-08-02 00:31:59 +0300
commitb9fca4f1323a76403c936c3dd9c94e458fac6bf9 (patch)
treea7fe051a3446d732a46ced226febbb2481a7681b /challenge-071/ash
parente7a6ffea01783979cc2e2d0f97ab8e8a3736e4a0 (diff)
downloadperlweeklychallenge-club-b9fca4f1323a76403c936c3dd9c94e458fac6bf9.tar.gz
perlweeklychallenge-club-b9fca4f1323a76403c936c3dd9c94e458fac6bf9.tar.bz2
perlweeklychallenge-club-b9fca4f1323a76403c936c3dd9c94e458fac6bf9.zip
ash 071
Diffstat (limited to 'challenge-071/ash')
-rw-r--r--challenge-071/ash/blog.txt1
-rw-r--r--challenge-071/ash/raku/ch-1.raku15
-rw-r--r--challenge-071/ash/raku/ch-2.raku46
3 files changed, 62 insertions, 0 deletions
diff --git a/challenge-071/ash/blog.txt b/challenge-071/ash/blog.txt
new file mode 100644
index 0000000000..046318945c
--- /dev/null
+++ b/challenge-071/ash/blog.txt
@@ -0,0 +1 @@
+https://andrewshitov.com/2020/08/01/raku-challenge-week-71/
diff --git a/challenge-071/ash/raku/ch-1.raku b/challenge-071/ash/raku/ch-1.raku
new file mode 100644
index 0000000000..d2f93d6b3e
--- /dev/null
+++ b/challenge-071/ash/raku/ch-1.raku
@@ -0,0 +1,15 @@
+#!/usr/bin/env raku
+
+# Task 1 from
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-071/
+
+# Comments: https://andrewshitov.com/2020/08/01/raku-challenge-week-71/
+
+my $n = 20;
+my @data = map {50.rand.Int}, ^$n;
+say @data;
+
+my @peaks = map {@data[$_]}, grep {
+ @data[$_ - 1] < @data[$_] > @data[$_ + 1]
+}, 1 .. $n - 2;
+say @peaks;
diff --git a/challenge-071/ash/raku/ch-2.raku b/challenge-071/ash/raku/ch-2.raku
new file mode 100644
index 0000000000..9a6daa118f
--- /dev/null
+++ b/challenge-071/ash/raku/ch-2.raku
@@ -0,0 +1,46 @@
+#!/usr/bin/env raku
+
+# Task 2 from
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-071/
+
+# Comments: https://andrewshitov.com/2020/08/01/raku-challenge-week-71/
+
+unit sub MAIN(Int $N is copy where * > 0 = 3, Int $size where * > 0 = 10);
+# Cannot use 'is rw' on optional parameter '$N'.
+
+class Node {
+ has $.data;
+ has $.next is rw;
+}
+
+# Create a random linked list. Note that it generates the list
+# from its tail towards the head.
+my $head;
+$head = Node.new(data => 100.rand.Int, next => $head) for ^$size;
+
+say $head;
+
+# Get the lengths of the list.
+my $length = 1;
+my $curr = $head;
+$length++ while $curr = $curr.next;
+
+# Expect $size == $length :-)
+# say $length;
+
+if $N >= $size {
+ $head = $head.next;
+}
+else {
+ $N--; # Input "1" means the element with 0 offset from the end;
+ $N = $size - $N; # Count from the end
+ $curr = $head;
+ my $prev = Nil;
+ while --$N {
+ $prev = $curr;
+ $curr = $curr.next;
+ }
+ $prev.next = $curr.next;
+}
+
+say $head;