aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoger Bell_West <roger@firedrake.org>2020-07-27 10:32:11 +0100
committerRoger Bell_West <roger@firedrake.org>2020-07-27 10:32:11 +0100
commit87cb3c5ff91c57a3a030889c3b0a09d7bb521873 (patch)
treecc08a13b666cec166bb54e27a3025eba51e104b7
parent6b0fef6d35e61345ad5fd3f2ab5847c0afafc5ff (diff)
downloadperlweeklychallenge-club-87cb3c5ff91c57a3a030889c3b0a09d7bb521873.tar.gz
perlweeklychallenge-club-87cb3c5ff91c57a3a030889c3b0a09d7bb521873.tar.bz2
perlweeklychallenge-club-87cb3c5ff91c57a3a030889c3b0a09d7bb521873.zip
Solution to challenge #71.1.
-rw-r--r--challenge-071/roger-bell-west/perl/ch-1.pl37
-rw-r--r--challenge-071/roger-bell-west/raku/ch-1.p631
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-071/roger-bell-west/perl/ch-1.pl b/challenge-071/roger-bell-west/perl/ch-1.pl
new file mode 100644
index 0000000000..1917324d6e
--- /dev/null
+++ b/challenge-071/roger-bell-west/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+use List::Util qw(shuffle);
+use Test::More tests => 2;
+
+is_deeply(peaks(18, 45, 38, 25, 10, 7, 21, 6, 28, 48),
+ [45, 21, 48],
+ 'peaks 1',
+ );
+
+is_deeply(peaks(47, 11, 32, 8, 1, 9, 39, 14, 36, 23),
+ [47, 32, 39, 36],
+ 'peaks 2',
+ );
+
+sub genseq {
+ my $n=shift;
+ my @out=shuffle(1..50);
+ splice @out,$n;
+ return @out;
+}
+
+sub peaks {
+ my @list=@_;
+ my @out;
+ foreach my $n (0..$#list) {
+ if (($n==0 || $list[$n]>$list[$n-1])
+ &&
+ ($n==$#list || $list[$n]>$list[$n+1])) {
+ push @out,$list[$n];
+ }
+ }
+ return \@out;
+}
diff --git a/challenge-071/roger-bell-west/raku/ch-1.p6 b/challenge-071/roger-bell-west/raku/ch-1.p6
new file mode 100644
index 0000000000..44dcb5378a
--- /dev/null
+++ b/challenge-071/roger-bell-west/raku/ch-1.p6
@@ -0,0 +1,31 @@
+#! /usr/bin/perl6
+
+use Test;
+
+plan 2;
+
+is-deeply(peaks((18, 45, 38, 25, 10, 7, 21, 6, 28, 48).List),
+ (45, 21, 48),
+ 'peaks 1',
+ );
+
+is-deeply(peaks((47, 11, 32, 8, 1, 9, 39, 14, 36, 23).List),
+ (47, 32, 39, 36),
+ 'peaks 2',
+ );
+
+sub genseq($n) {
+ return (1..50).pick($n);
+}
+
+sub peaks(@list) {
+ my @out;
+ for (0..@list.end) -> $n {
+ if (($n==0 || @list[$n] > @list[$n-1])
+ &&
+ ($n==@list.end || @list[$n] > @list[$n+1])) {
+ push @out,@list[$n];
+ }
+ }
+ return @out.flat;
+}