diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-03-29 17:10:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-29 17:10:40 +0100 |
| commit | fc90d86a5671c9030f8441321155feae1b6fed88 (patch) | |
| tree | 465a3a480111da94443728d19a1305879a60d385 | |
| parent | e2dda629907df072528fa79b6efd0af6a79fe657 (diff) | |
| parent | 4769d55c19fe240805c7d58354a0178729aea99e (diff) | |
| download | perlweeklychallenge-club-fc90d86a5671c9030f8441321155feae1b6fed88.tar.gz perlweeklychallenge-club-fc90d86a5671c9030f8441321155feae1b6fed88.tar.bz2 perlweeklychallenge-club-fc90d86a5671c9030f8441321155feae1b6fed88.zip | |
Merge pull request #1480 from drclaw1394/master
Ruben's solutions to w53 ch-1 and ch-2. perl and raku
| -rw-r--r-- | challenge-053/ruben-westerberg/README | 8 | ||||
| -rwxr-xr-x | challenge-053/ruben-westerberg/perl/ch-1.pl | 43 | ||||
| -rwxr-xr-x | challenge-053/ruben-westerberg/perl/ch-2.pl | 58 | ||||
| -rwxr-xr-x | challenge-053/ruben-westerberg/raku/ch-1.raku | 34 | ||||
| -rwxr-xr-x | challenge-053/ruben-westerberg/raku/ch-2.raku | 34 |
5 files changed, 174 insertions, 3 deletions
diff --git a/challenge-053/ruben-westerberg/README b/challenge-053/ruben-westerberg/README index 84ce79a25d..37aacdf0d8 100644 --- a/challenge-053/ruben-westerberg/README +++ b/challenge-053/ruben-westerberg/README @@ -2,9 +2,11 @@ Solution by Ruben Westerberg ch-1.pl and ch-1.raku =================== -Stepping numbers -Run the the program with two arguments between 100 and 999. Displays stepping numbers. +Rotate matrix +Run program to demonstrate rotating matrix by 90, 180 and 270 deg + ch-2.pl and ch-2.raku =================== -Coin picking game. Use the < and > keys to select the end of the coin list. Plays against the computer. Most money wins +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. diff --git a/challenge-053/ruben-westerberg/perl/ch-1.pl b/challenge-053/ruben-westerberg/perl/ch-1.pl new file mode 100755 index 0000000000..48f1cf9509 --- /dev/null +++ b/challenge-053/ruben-westerberg/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Data::Dumper; +use Math::Trig ':pi'; +use POSIX qw<round>; +use v5.28; + +my $matrix=[ + [ 1, 2, 3 ], + [ 4, 5, 6 ], + [ 7, 8, 9 ] +]; + + +say "0deg rotation:"; +rotate($matrix,0); +say "90deg rotation:"; +rotate($matrix,90); +say "180deg rotation:"; +rotate($matrix,180); +say "270deg rotation:"; +rotate($matrix,270); + +sub rotate { + my ($input,$angle)=@_; + #input indexing + my @ip=map {pip4 *$_} 0..7; + my @ir=map {1+round sin} @ip; + my @ic=map {1+round cos} @ip; + + #output indexing + my @polar=map {($angle/180 *pi) + pip4 *$_} 0..7; + my @row= map {1+round sin} @polar; + my @col =map {1+round cos} @polar; + + my $out=[[],[],[]]; + + $out->[1]->[1]=$matrix->[1]->[1]; + $out->[$row[$_]]->[$col[$_]]=$matrix->[$ir[$_]]->[$ic[$_]] for (0..@col-1); + say "[".join(", ", @{$out->[$_]})."]" for 0..2; + $out; +} diff --git a/challenge-053/ruben-westerberg/perl/ch-2.pl b/challenge-053/ruben-westerberg/perl/ch-2.pl new file mode 100755 index 0000000000..9aa0c3ba43 --- /dev/null +++ b/challenge-053/ruben-westerberg/perl/ch-2.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl +use warnings; +use strict; +use List::Util; +use v5.26; +no warnings 'experimental'; + +my $n=$ARGV[0]//2; +my @vowels=qw<a e i o u>; + +die "Number not in range 1..5\n" unless grep /$n/, 1..5; + +say for sort map {join "",@$_} grep { + my $sum=0; + my $s=join '', @$_; + given ($s) { + my @match; #<<Used to force list context + $sum+=@match=/a/g; #<<Count all a + $sum-=@match=(/a[ie]|a$/g); #<<make sure the rules match + #<<Net sum is 0 for success + $sum+=@match=/e/g; + $sum-= @match=(/ei|e$/g); + + $sum+=@match=/i/g; + $sum-= @match=/i[aeou]|i$/g; + + + $sum+=@match=/o/g; + $sum-=@match=/o[au]|o$/g; + + $sum+=@match=/u/g; + $sum-=@match=/u[oe]|u$/g; + } + $sum==0; +} combinations(\@vowels,$n); + + +=item combinations +=cut +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]); + #print $count,"\n";; + 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-053/ruben-westerberg/raku/ch-1.raku b/challenge-053/ruben-westerberg/raku/ch-1.raku new file mode 100755 index 0000000000..5d4d8ccc3e --- /dev/null +++ b/challenge-053/ruben-westerberg/raku/ch-1.raku @@ -0,0 +1,34 @@ +#!/usr/bin/env raku +my $matrix=[ + [ 1, 2, 3 ], + [ 4, 5, 6 ], + [ 7, 8, 9 ] +]; + +say "0deg rotation:"; +rotate($matrix,0); +say "90deg rotation:"; +rotate($matrix,90); +say "180deg rotation:"; +rotate($matrix,180); +say "270deg rotation:"; +rotate($matrix,270); + +sub rotate($input,$angle) { + #input indexing + my @ip= map {pi/4 *$_}, 0..7; + my @ir= map {1+round .sin}, @ip; #no sincos func in raku? + my @ic= map {1+round .cos}, @ip; #.. feature request! + + #output indexing + my @polar=map {($angle/180 *pi) + pi/4 *$_}, 0..7; + my @row= map {1+round .sin}, @polar; + my @col =map {1+round .cos}, @polar; + + my $out=[[],[],[]]; + + $out[1][1]=$matrix[1][1]; + $out[@row[$_]][@col[$_]]=$matrix[@ir[$_]][@ic[$_]] for (0..^@col); + say "["~join(", ", $out[$_])~"]" for 0..2; + $out; +} diff --git a/challenge-053/ruben-westerberg/raku/ch-2.raku b/challenge-053/ruben-westerberg/raku/ch-2.raku new file mode 100755 index 0000000000..d0d4191303 --- /dev/null +++ b/challenge-053/ruben-westerberg/raku/ch-2.raku @@ -0,0 +1,34 @@ +#!/usr/bin/env raku + +my $n=@*ARGS[0]//2; +my @vowels= <a e i o u>; + +die "Number not in range 1..5\n" unless $n ~~ 1..5; + +@vowels.combinations ==> #<<Straight combos +map {|.permutations} ==> #<<create different orderings +grep {.elems == $n} ==> #<<Only want length $n +grep { + my $sum=0; + my $s=join '', @$_; + given ($s) { + $sum+=m:g/a/; + $sum-=m:g/a<[ie]>|a$/; + + $sum+=m:g/e/; + $sum-= (m:g/ei|e$/); + + $sum+=m:g/i/; + $sum-= m:g/i<[aeou]>|i$/; + + $sum+=m:g/o/; + $sum-=m:g/o<[au]>|o$/; + + $sum+=m:g/u/; + $sum-=m:g/u<[oe]>|u$/; + } + $sum==0; +} ==> +my @combinations; + +.say for @combinations.map: {.join} ==> sort |
