aboutsummaryrefslogtreecommitdiff
path: root/challenge-055
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-04-12 14:48:37 +0100
committerGitHub <noreply@github.com>2020-04-12 14:48:37 +0100
commite0cd79c32be6bf2c4d8cf53ba50ab5b2f43af9fe (patch)
treeb74d176933a5022a75917318629d5fd515cd8bd2 /challenge-055
parentd7c8cc61c96c588f47d4e2b65e8fcbe15ced6277 (diff)
parent393d72adb35ffbf6ab04acaa62072e37b60a2bee (diff)
downloadperlweeklychallenge-club-e0cd79c32be6bf2c4d8cf53ba50ab5b2f43af9fe.tar.gz
perlweeklychallenge-club-e0cd79c32be6bf2c4d8cf53ba50ab5b2f43af9fe.tar.bz2
perlweeklychallenge-club-e0cd79c32be6bf2c4d8cf53ba50ab5b2f43af9fe.zip
Merge pull request #1552 from drclaw1394/master
Ruben's solutions to w55 ch-1 and ch-2. perl and raku
Diffstat (limited to 'challenge-055')
-rw-r--r--challenge-055/ruben-westerberg/README10
-rwxr-xr-xchallenge-055/ruben-westerberg/perl/ch-1.pl34
-rwxr-xr-xchallenge-055/ruben-westerberg/perl/ch-2.pl42
-rwxr-xr-xchallenge-055/ruben-westerberg/raku/ch-1.raku29
-rwxr-xr-xchallenge-055/ruben-westerberg/raku/ch-2.raku22
5 files changed, 131 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-1.pl b/challenge-055/ruben-westerberg/perl/ch-1.pl
new file mode 100755
index 0000000000..408c767ff1
--- /dev/null
+++ b/challenge-055/ruben-westerberg/perl/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+use warnings;
+use strict;
+use feature qw<:all>;
+no warnings "experimental";
+use POSIX qw<round>;
+my $n=$ARGV[0]//3;
+my $number=join "",map { round rand 1 } 1..$n;
+say "Testing on: $number";
+my $maxSize=-1;
+my @max;
+for (my $l=0;$l<$n;$l++) {
+ for (my $r=$l;$r<$n;$r++) {
+ my $str=$number;
+ substr($str,$l,$r-$l+1)=~ tr/10/01/;
+ given (my @a=$str=~/1/g ) {
+ when ($maxSize) {
+ push @max, [$l,$r];
+ }
+
+ when ($_>$maxSize) {
+ @max=([$l,$r]);
+ $maxSize=$_;
+ }
+ default {
+
+ }
+ }
+
+ }
+}
+say "Maximal 1's of count $maxSize given by L/R pairs:";
+say join ", ", @$_ for @max;
+
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-1.raku b/challenge-055/ruben-westerberg/raku/ch-1.raku
new file mode 100755
index 0000000000..ce1b8b886b
--- /dev/null
+++ b/challenge-055/ruben-westerberg/raku/ch-1.raku
@@ -0,0 +1,29 @@
+#!/usr/bin/env raku
+
+my $n=@*ARGS[0]//3;
+my $number=(1.rand.round xx $n).join: "";
+say "Testing on: $number";
+my $maxSize=-1;
+my @max;
+loop (my $l=0;$l < $n; $l++) {
+ loop (my $r=$l;$r < $n;$r++) {
+ my $str=$number;
+ tr/10/01/ given $str.substr-rw($l,$r-$l+1);
+ given (m:g/1/ given $str ).Int {
+ when $maxSize {
+ push @max, [$l,$r];
+ }
+
+ when ($_>$maxSize) {
+ @max=[[$l,$r],];
+ $maxSize=$_;
+ }
+ default {
+
+ }
+ }
+ }
+}
+say "Maximal 1's of count $maxSize given by L/R pairs:";
+say @$_.join: ", " for @max;
+
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;