diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-03-23 12:08:27 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-03-23 12:08:27 +0000 |
| commit | b286e395704d7b336087f377630657089a5fe7ab (patch) | |
| tree | 4a721d8c7aa9fa1be8a83b609151380b8a79cc1d /challenge-053 | |
| parent | 8ac2a0e9d2aebfb3ac51b8d26fcf3973fd44bb70 (diff) | |
| download | perlweeklychallenge-club-b286e395704d7b336087f377630657089a5fe7ab.tar.gz perlweeklychallenge-club-b286e395704d7b336087f377630657089a5fe7ab.tar.bz2 perlweeklychallenge-club-b286e395704d7b336087f377630657089a5fe7ab.zip | |
- Added solutions by Javier Luque.
Diffstat (limited to 'challenge-053')
| -rw-r--r-- | challenge-053/javier-luque/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-053/javier-luque/perl/ch-1.pl | 51 | ||||
| -rw-r--r-- | challenge-053/javier-luque/perl/ch-2.pl | 48 | ||||
| -rw-r--r-- | challenge-053/javier-luque/raku/ch-1.p6 | 47 | ||||
| -rw-r--r-- | challenge-053/javier-luque/raku/ch-2.p6 | 50 |
5 files changed, 197 insertions, 0 deletions
diff --git a/challenge-053/javier-luque/blog.txt b/challenge-053/javier-luque/blog.txt new file mode 100644 index 0000000000..b576c1f03f --- /dev/null +++ b/challenge-053/javier-luque/blog.txt @@ -0,0 +1 @@ +https://perlchallenges.wordpress.com/2020/03/23/perl-weekly-challenge-053/ diff --git a/challenge-053/javier-luque/perl/ch-1.pl b/challenge-053/javier-luque/perl/ch-1.pl new file mode 100644 index 0000000000..21a0d038eb --- /dev/null +++ b/challenge-053/javier-luque/perl/ch-1.pl @@ -0,0 +1,51 @@ +#!/usr/bin/perl +# Test: ./ch-1.pl +use strict; +use warnings; +use feature qw /say/; + +# 3 x 3 matrix +my $three = [ + [1,2,3], + [4,5,6], + [7,8,9], +]; + +say 'Original '; +print_matrix($three); + +for (my $i = 1; $i < 4; $i++) { + say 'Rotate ' . 90 * $i; + rotate_matrix($three); + print_matrix($three); +} + +# Rotate the matrix +sub rotate_matrix { + my $m = shift; + + # Size of the matrix + my $n = scalar(@$m); + + for (my $i = 0; $i < int($n / 2); $i++) { + for (my $j = $i; $j < $n - $i - 1; $j++) { + my $temp = $m->[$i]->[$j]; + $m->[$i]->[$j] = $m->[$n-$j-1]->[$i]; + $m->[$n-$j-1]->[$i] = $m->[$n-$i-1]->[$n-$j-1]; + $m->[$n-$i-1]->[$n-$j-1] = $m->[$j]->[$n-$i-1]; + $m->[$j]->[$n-$i-1] = $temp; + } + } +} + +# Print the matrix +sub print_matrix { + my $m = shift; + + # Max length of the attributes + my $length = length(scalar(@$m)**2) + 1; + + for my $row (@$m) { + say map { sprintf ( " %${length}d", $_ ) } @$row; + } +} diff --git a/challenge-053/javier-luque/perl/ch-2.pl b/challenge-053/javier-luque/perl/ch-2.pl new file mode 100644 index 0000000000..bf66f16c10 --- /dev/null +++ b/challenge-053/javier-luque/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl +# Test: ./ch-2.pl 3 +use strict; +use warnings; +use feature qw /say/; +use Algorithm::Combinatorics qw(variations_with_repetition); + +my $size = $ARGV[0] || 5; +my @vowels = ('a', 'e', 'i', 'o', 'u'); + +my $iter = variations_with_repetition(\@vowels,$size); +while (my $v = $iter->next) { + say join '', @$v + if (valid_combination($v)); +} + +# IS valid combination +sub valid_combination { + my $word = shift; + + # Faster than a regex + for (my $i = 0; $i < scalar(@$word) - 1; $i++) { + return 0 unless + _check_letters($word, $i, 'a', 'e', 'i') && + _check_letters($word, $i, 'e', 'i') && + _check_letters($word, $i, 'i', 'a', 'e', 'o', 'u') && + _check_letters($word, $i, 'o', 'a', 'u') && + _check_letters($word, $i, 'u', 'o', 'e'); + } + + return 1; +} + +# Check the folowing letters +sub _check_letters { + my ($word, $i, $letter, @checks) = @_; + my $valid = 1; + + if ($word->[$i] eq $letter) { + $valid = 0; + for my $check (@checks) { + $valid = 1 + if ($word->[$i + 1] eq $check); + } + } + + return $valid; +} diff --git a/challenge-053/javier-luque/raku/ch-1.p6 b/challenge-053/javier-luque/raku/ch-1.p6 new file mode 100644 index 0000000000..efdd256efe --- /dev/null +++ b/challenge-053/javier-luque/raku/ch-1.p6 @@ -0,0 +1,47 @@ +#!/usr/bin/perl +# Test: ./ch-6.p6 + +multi MAIN() { + + # 3 x 3 matrix + my @three = ( + [1,2,3], + [4,5,6], + [7,8,9] + ); + + say 'Original '; + print-matrix(@three); + + loop (my $i = 1; $i < 4; $i++) { + say 'Rotate ' ~ 90 * $i; + rotate-matrix(@three); + print-matrix(@three) ; + } +} + +# Rotate the matrix +sub rotate-matrix(@m) { + my $n = @m.elems; + + loop (my $i = 0; $i < Int($n / 2); $i++) { + loop (my $j = $i; $j < $n - $i - 1; $j++) { + my $temp = @m[$i; $j]; + @m[$i; $j] = @m[$n-$j-1; $i]; + @m[$n-$j-1; $i] = @m[$n-$i-1; $n-$j-1]; + @m[$n-$i-1; $n-$j-1] = @m[$j; $n-$i-1]; + @m[$j; $n-$i-1] = $temp; + } + } +} + +# Print the matrix +sub print-matrix(@m) { + for (@m) -> $row { + say $row.map(-> + $value { + sprintf("%3d", $value) + } + ).join; + } +} diff --git a/challenge-053/javier-luque/raku/ch-2.p6 b/challenge-053/javier-luque/raku/ch-2.p6 new file mode 100644 index 0000000000..358630f5b5 --- /dev/null +++ b/challenge-053/javier-luque/raku/ch-2.p6 @@ -0,0 +1,50 @@ +# Test: perl6 ch-2.p6 +multi MAIN { MAIN(2) }; + +multi MAIN(Int $size) { + # Generate the possible combinations + my @vowels; + push @vowels, 'a', 'e', 'i', 'o', 'u' + for (1 .. $size); + my @combos = @vowels.combinations: $size; + + # Check each combination + my @solutions; + for @combos.unique -> @combo { + push @solutions, @combo.join + if (valid-combination(@combo)); + } + + # Print the solutions + .say for @solutions.unique.sort; +} + +# IS valid combination +sub valid-combination(@word) { + # Faster than a regex + loop (my $i = 0; $i < @word.elems - 1; $i++) { + return 0 unless + _check-letters(@word, $i, 'a', ['e','i']) && + _check-letters(@word, $i, 'e', ['i']) && + _check-letters(@word, $i, 'i', ['a', 'e', 'o', 'u']) && + _check-letters(@word, $i, 'o', ['a', 'u']) && + _check-letters(@word, $i, 'u', ['o', 'e']); + } + + return 1; +} + +# Check the folowing letters +sub _check-letters(@word, Int $i, $letter, @checks) { + my $valid = True; + + if (@word[$i] eq $letter) { + $valid = False; + for (@checks) -> $check { + $valid = True + if (@word[$i + 1] eq $check); + } + } + + return $valid; +} |
