diff options
| author | Ruben Westerberg <drclaw@mac.com> | 2020-04-05 20:14:27 +1000 |
|---|---|---|
| committer | Ruben Westerberg <drclaw@mac.com> | 2020-04-05 20:14:27 +1000 |
| commit | 0b06f21dc8d88dfe2dfbbbf3966203103ac2ca2c (patch) | |
| tree | d849ab23c6e59fcb784c7b6628a975d2c5322875 | |
| parent | db5c794bc246e2361ed344e2006827ac0d1fc723 (diff) | |
| download | perlweeklychallenge-club-0b06f21dc8d88dfe2dfbbbf3966203103ac2ca2c.tar.gz perlweeklychallenge-club-0b06f21dc8d88dfe2dfbbbf3966203103ac2ca2c.tar.bz2 perlweeklychallenge-club-0b06f21dc8d88dfe2dfbbbf3966203103ac2ca2c.zip | |
Added solutions for w54 ch1 and ch2. perl and raku
| -rw-r--r-- | challenge-054/ruben-westerberg/README | 10 | ||||
| -rwxr-xr-x | challenge-054/ruben-westerberg/perl/ch-1.pl | 52 | ||||
| -rwxr-xr-x | challenge-054/ruben-westerberg/perl/ch-2.pl | 40 | ||||
| -rwxr-xr-x | challenge-054/ruben-westerberg/raku/ch-1.raku | 17 | ||||
| -rwxr-xr-x | challenge-054/ruben-westerberg/raku/ch-2.raku | 39 |
5 files changed, 154 insertions, 4 deletions
diff --git a/challenge-054/ruben-westerberg/README b/challenge-054/ruben-westerberg/README index 37aacdf0d8..b8b73d9e4b 100644 --- a/challenge-054/ruben-westerberg/README +++ b/challenge-054/ruben-westerberg/README @@ -2,11 +2,13 @@ Solution by Ruben Westerberg ch-1.pl and ch-1.raku =================== -Rotate matrix -Run program to demonstrate rotating matrix by 90, 180 and 270 deg +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 + ch-2.pl and ch-2.raku =================== -Vowels -Run the program with a single command line argument between 1 and 5 (default is 2). Program generates list of strings of this length from vowels and which abide by the specified rules. +Demonstrates the longest 20 Collatz Sequences for staring numbers 1..n. n is specified on the comand line. Otherwise 23 is used by default. diff --git a/challenge-054/ruben-westerberg/perl/ch-1.pl b/challenge-054/ruben-westerberg/perl/ch-1.pl new file mode 100755 index 0000000000..18de9a70c0 --- /dev/null +++ b/challenge-054/ruben-westerberg/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(switch say); +no warnings 'experimental'; +use List::Util; + + +my ($n,$k)=($ARGV[0]//3, $ARGV[1]//4); + +#Error checking. Limit the value of k to the maximum permutation count +die "n and k must be >= 1)" if $n < 1 or $k < 1; +my $pCount= factorial($n); +die "k must be lest than n! = $pCount" if $k>$pCount; + +#actually select the kth permutation +my @perm=sort {$a > $b} map {join "", @$_} combinations([1..$n], $n); +say $perm[$k-1]; + + + +sub factorial { + my ($n)=@_; + do { + given ($n) { + 1 when 0; + default { + List::Util::reduce { $a*$b} 1..$n; + } + } + } +} + +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-054/ruben-westerberg/perl/ch-2.pl b/challenge-054/ruben-westerberg/perl/ch-2.pl new file mode 100755 index 0000000000..a0169c6d1b --- /dev/null +++ b/challenge-054/ruben-westerberg/perl/ch-2.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw<say switch>; +no warnings qw<experimental recursion>; + +my $max=$ARGV[0]//23; #sane default without cmd line args +my @seqs=( ([]) x 20); #Initalise the largest 20 sequences found + +for ( 1..$max) { + my $s=collaz([int($_)]); + for my $i (0..@seqs-1) { + if (@$s > @{$seqs[$i]}) { + pop @seqs unless @seqs < 20; + splice @seqs,$i,0,$s; + last; + } + } +}; + +say "Top 20 Collaz Sequence lengths for starting numbers 1..$max"; +for(grep { @$_ != 0} @seqs) { + printf "Starting Number: %10d Sequence Length: %d\n", $_->[0],scalar @$_; +} + + + +sub collaz { + my ($seq)=@_; + given ($seq->[-1]) { + when ($_%2 == 0) { + push @$seq, $_/2; + } + default { + push @$seq, 3*$_+1; + } + } + &collaz unless $seq->[-1]==1; + $seq; +} diff --git a/challenge-054/ruben-westerberg/raku/ch-1.raku b/challenge-054/ruben-westerberg/raku/ch-1.raku new file mode 100755 index 0000000000..ccbe92c06d --- /dev/null +++ b/challenge-054/ruben-westerberg/raku/ch-1.raku @@ -0,0 +1,17 @@ +#!/usr/bin/env raku +use MONKEY-TYPING; +augment class Int { + method factorial() { + self==0??1!! [*] (1..self); + } +} + +my ($n,$k)=(Int(@*ARGS[0]//3), Int(@*ARGS[1]//4)); +die "n and k must be >= 1)" if $n < 1 or $k < 1; +my $pCount= $n.factorial/($n-$k).factorial; +die "k must be lest than n! = $pCount" if $k>$pCount; + +#actually select the kth permutation +say (1..$n).permutations[$k-1].join; + + diff --git a/challenge-054/ruben-westerberg/raku/ch-2.raku b/challenge-054/ruben-westerberg/raku/ch-2.raku new file mode 100755 index 0000000000..74d673098b --- /dev/null +++ b/challenge-054/ruben-westerberg/raku/ch-2.raku @@ -0,0 +1,39 @@ +#!/usr/bin/env raku + +my $max=@*ARGS[0]//23; #sane default without cmd line args +my @seqs=( [] xx 20); #Initalise the largest 20 sequences found +my $l=Lock.new; #Create a lock to allow sequential access to top 20 + +(1..$max).hyper(batch=>1000, degree=>8).map: { + my $s=collaz([$_ ,]); + $l.protect({ + for (0..^@seqs) -> $i { + if $s.elems > @seqs[$i].elems { + @seqs.pop unless @seqs.elems < 20; + @seqs.splice($i,0,[$s]); + last; + } + } + }); + (); +}; + +say "Top 20 Collaz Sequence lengths for starting numbers 1..$max"; +for @seqs.grep: *.elems != 0 { + printf "Starting Number: %10d Sequence Length: %d\n", .[0],.elems; +} + + + +sub collaz ($seq) { + given $seq[*-1] { + when $_%%2 { + $seq.push: $_/2; + } + default { + $seq.push: 3*$_+1; + } + } + collaz($seq) unless $seq[*-1]==1; + $seq; +} |
