aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Proctor <simon.proctor@zoopla.co.uk>2020-04-06 11:46:32 +0100
committerSimon Proctor <simon.proctor@zoopla.co.uk>2020-04-06 11:46:32 +0100
commit256495ff4a2ea4b3c2c7ef1414d89c8de73f248e (patch)
tree377747b5413ab8c455e5aff78729329e939ed479
parent906abbd5a71cbcce81abf659cc7bb36ecfd06eca (diff)
downloadperlweeklychallenge-club-256495ff4a2ea4b3c2c7ef1414d89c8de73f248e.tar.gz
perlweeklychallenge-club-256495ff4a2ea4b3c2c7ef1414d89c8de73f248e.tar.bz2
perlweeklychallenge-club-256495ff4a2ea4b3c2c7ef1414d89c8de73f248e.zip
Here we go, note it can take a while with longer lists
-rw-r--r--challenge-055/simon-proctor/raku/ch-2.p639
1 files changed, 39 insertions, 0 deletions
diff --git a/challenge-055/simon-proctor/raku/ch-2.p6 b/challenge-055/simon-proctor/raku/ch-2.p6
new file mode 100644
index 0000000000..086bdfa95d
--- /dev/null
+++ b/challenge-055/simon-proctor/raku/ch-2.p6
@@ -0,0 +1,39 @@
+#!/usr/bin/env raku
+
+use v6;
+
+#| Make a random list of length N integers between -100 and 100 then give the wave sorted versions
+multi sub MAIN (
+ UInt $n where 2 <= * <= 20 #= Positive integer 2 <= N <= 20
+) {
+ MAIN( |(-100..100).roll($n) );
+}
+
+multi sub MAIN( *@ ) is hidden-from-USAGE { say "Please give a list of 2 or more items or a single integer greater then 1";say $*USAGE; }
+
+#| Given a list of integers return all the wave sorted lists
+multi sub MAIN( *@input where { $_.elems >= 2 && $_.all ~~ Int } ) {
+ .say for unique-perms( @input ).race.grep( -> @l { is-wave( 'lte', |@l ) } );
+}
+
+sub unique-perms ( @input ) {
+ my %seen;
+ my @perms = @input.permutations;
+ gather {
+ while ( @perms ) {
+ my $next = @perms.shift;
+ next if %seen{$next}:exists;
+ %seen{$next} = True;
+ take $next;
+ }
+ }
+}
+
+multi sub is-wave( 'lte', Int $a, Int $b where { $a <= $b } ) { True }
+multi sub is-wave( 'lte', Int $a, Int $b where { $a > $b } ) { False }
+multi sub is-wave( 'gte', Int $a, Int $b where { $a >= $b } ) { True }
+multi sub is-wave( 'gte', Int $a, Int $b where { $a < $b } ) { False }
+multi sub is-wave( 'gte', Int $a, Int $b where { $b > $a }, *@ ) { False }
+multi sub is-wave( 'lte', Int $a, Int $b where { $a > $b }, *@ ) { False }
+multi sub is-wave( 'gte', Int $a, Int $b where { $a >= $b }, $c, *@r ) { True && is-wave( 'lte', $b, $c, |@r ); }
+multi sub is-wave( 'lte', Int $a, Int $b where { $a <= $b }, $c, *@r ) { True && is-wave( 'gte', $b, $c, |@r ); }