aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-11-22 23:07:53 +0000
committerGitHub <noreply@github.com>2020-11-22 23:07:53 +0000
commit6faca325b9bfc861dc77e4a099c585f3b216e766 (patch)
tree678ecc3886cc07c7ef711ddfb0aa519083b29b85
parenteaa6d030661f7e3684a02b8e1b56b2f074a3698e (diff)
parent78e8b6c7a680b43772ee439b1343f4e6e63a34a0 (diff)
downloadperlweeklychallenge-club-6faca325b9bfc861dc77e4a099c585f3b216e766.tar.gz
perlweeklychallenge-club-6faca325b9bfc861dc77e4a099c585f3b216e766.tar.bz2
perlweeklychallenge-club-6faca325b9bfc861dc77e4a099c585f3b216e766.zip
Merge pull request #2814 from wambash/challenge-week-087
Challenge week 087 — ch-1
-rw-r--r--challenge-087/wambash/raku/ch-1.raku33
1 files changed, 33 insertions, 0 deletions
diff --git a/challenge-087/wambash/raku/ch-1.raku b/challenge-087/wambash/raku/ch-1.raku
new file mode 100644
index 0000000000..a525eeed56
--- /dev/null
+++ b/challenge-087/wambash/raku/ch-1.raku
@@ -0,0 +1,33 @@
+#!/usr/bin/env raku
+
+sub consecutive-sequence-reducer ( @n, $b ) {
+ with @n {
+ when .[0][0] -1 == $b { ($b,|.head), |.skip }
+ default { ($b,) , |$_ }
+ }
+}
+
+sub longest-consecutive-sequence (+@n) {
+ @n
+ andthen .sort
+ andthen .reverse
+ andthen .cache
+ andthen ((.head), ), |.skip
+ andthen .reduce: &consecutive-sequence-reducer
+ andthen .max: *.elems
+ andthen .elems > 1 ?? $_ !! 0
+}
+
+multi MAIN(*@n) {
+ say longest-consecutive-sequence @n
+}
+
+multi MAIN (Bool :$test!) {
+ use Test;
+ is-deeply consecutive-sequence-reducer(( (2,3), ), 1), ((1,2,3), );
+ is-deeply consecutive-sequence-reducer(( (2,3), ), 0), ((0,), (2,3));
+ is longest-consecutive-sequence(100, 4, 50, 3, 2), (2, 3, 4);
+ is longest-consecutive-sequence( 20, 30, 10, 40, 50), 0;
+ is longest-consecutive-sequence( 20, 19, 9, 11, 10), (9, 10, 11);
+ done-testing;
+}