aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Westerberg <drclaw@mac.com>2020-04-12 22:57:01 +1000
committerRuben Westerberg <drclaw@mac.com>2020-04-12 22:57:01 +1000
commit393d72adb35ffbf6ab04acaa62072e37b60a2bee (patch)
tree592ad2a11d194126278d1876dda3e025b1210315
parent9cb12de0eb76b02d3f0a2f68cd0dc5a80486c8d2 (diff)
downloadperlweeklychallenge-club-393d72adb35ffbf6ab04acaa62072e37b60a2bee.tar.gz
perlweeklychallenge-club-393d72adb35ffbf6ab04acaa62072e37b60a2bee.tar.bz2
perlweeklychallenge-club-393d72adb35ffbf6ab04acaa62072e37b60a2bee.zip
Added ch-2 perl and raku
-rw-r--r--challenge-055/ruben-westerberg/README10
-rwxr-xr-xchallenge-055/ruben-westerberg/perl/ch-2.pl42
-rwxr-xr-xchallenge-055/ruben-westerberg/raku/ch-2.raku22
3 files changed, 68 insertions, 6 deletions
diff --git a/challenge-055/ruben-westerberg/README b/challenge-055/ruben-westerberg/README
index b8b73d9e4b..1d82ee775f 100644
--- a/challenge-055/ruben-westerberg/README
+++ b/challenge-055/ruben-westerberg/README
@@ -2,13 +2,11 @@ Solution by Ruben Westerberg
ch-1.pl and ch-1.raku
===================
-kth Permutation sequence
-Run the program with two commandline arguments (n and k) to generate the permutation of n integers. Program will display the kth permutation.
-Validity of input is also checked with k required to be less then the number of permutations possible for the value n
-With no inputs the value of n=3 and k=4 are used
-
+Flip binary
+Run program to find largest number of 1's in binary number, after a select sub set has been flipped. Accepts 1 optional argument, which is is the number of digits in the (random) number to test.
+3 is the default
ch-2.pl and ch-2.raku
===================
-Demonstrates the longest 20 Collatz Sequences for staring numbers 1..n. n is specified on the comand line. Otherwise 23 is used by default.
+Computes all the wave arrays for a given integer array. Accepts a single arbument which is the size of the array. Default is 4
diff --git a/challenge-055/ruben-westerberg/perl/ch-2.pl b/challenge-055/ruben-westerberg/perl/ch-2.pl
new file mode 100755
index 0000000000..2e11d61f7f
--- /dev/null
+++ b/challenge-055/ruben-westerberg/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+use List::Util;
+use v5.26;
+my $n=$ARGV[0]//4;
+my @number=1..$n;
+my @waves=grep {
+
+ my $res=1;
+ while (my ($k,$v)=each @$_) {
+ state $prev;
+ unless ($k) {
+ $prev=$v;
+ next;
+ }
+ my $op=(($k%2)==0) ? 1 : -1;
+ $res &&=($v-$prev)*$op > 0;
+ last unless $res;
+ $prev=$v;
+ }
+ $res?$_: ();
+}
+combinations(\@number,$n);
+say join ",", @$_ for @waves;
+
+sub combinations {
+ my @combinations=();
+ my ($data,$size)=@_;
+ my @indexes=(0) x ($size+1);;
+ my $i=0;
+ until ($indexes[$size]) {
+ my $count=List::Util::uniq(@indexes[0..$size-1]);
+ push @combinations, [@$data[@indexes[0..$size-1]]] if $count == $size;
+ $indexes[0]++;
+ for (0..$size-1) {
+ if ($indexes[$_] != 0 and 0 == ($indexes[$_] % @$data)) {
+ $indexes[$_]=0;
+ $indexes[$_+1]++;
+ }
+ }
+ }
+ @combinations;
+}
diff --git a/challenge-055/ruben-westerberg/raku/ch-2.raku b/challenge-055/ruben-westerberg/raku/ch-2.raku
new file mode 100755
index 0000000000..a04f3e5a75
--- /dev/null
+++ b/challenge-055/ruben-westerberg/raku/ch-2.raku
@@ -0,0 +1,22 @@
+#!/usr/bin/env raku
+
+my $n=@*ARGS[0]//4;
+my $number=1..$n;
+my @waves=gather {
+ for $number.permutations -> $p {
+ my $res=True;
+ for $p.kv -> $k, $v {
+ state $prev;
+ unless $k {
+ $prev=$v;
+ next;
+ }
+ my $op=$k%%2??1!!-1;
+ $res and= ($v-$prev)*$op > 0;
+ last unless $res;
+ $prev=$v;
+ }
+ take $p if $res;
+ }
+}
+.say for @waves;