diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-03-29 17:19:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-29 17:19:25 +0100 |
| commit | 0bd639e9df26d63d5464b363723d1b698e100b2e (patch) | |
| tree | bb458f740011d8bdc29f4366e1e6744c2cc1161c /challenge-053 | |
| parent | 386db2cfb35f2d4a947230bdefe26ad8aa957d86 (diff) | |
| parent | 7c1b8d0a062e7513af4922e9d4a0c5ae86a7b829 (diff) | |
| download | perlweeklychallenge-club-0bd639e9df26d63d5464b363723d1b698e100b2e.tar.gz perlweeklychallenge-club-0bd639e9df26d63d5464b363723d1b698e100b2e.tar.bz2 perlweeklychallenge-club-0bd639e9df26d63d5464b363723d1b698e100b2e.zip | |
Merge pull request #1481 from aliciabielsa/branch-for-challenge-053
challenge 053 perl and raku
Diffstat (limited to 'challenge-053')
| -rw-r--r-- | challenge-053/alicia-bielsa/perl/ch-1.pl | 60 | ||||
| -rw-r--r-- | challenge-053/alicia-bielsa/perl/ch-2.pl | 76 | ||||
| -rwxr-xr-x | challenge-053/alicia-bielsa/raku/ch-1.p6 | 65 | ||||
| -rw-r--r-- | challenge-053/alicia-bielsa/raku/ch-2.p6 | 64 |
4 files changed, 265 insertions, 0 deletions
diff --git a/challenge-053/alicia-bielsa/perl/ch-1.pl b/challenge-053/alicia-bielsa/perl/ch-1.pl new file mode 100644 index 0000000000..3fcf1a0ed0 --- /dev/null +++ b/challenge-053/alicia-bielsa/perl/ch-1.pl @@ -0,0 +1,60 @@ +use strict; +use warnings; + +# Rotate Matrix +# Write a script to rotate the followin matrix by given 90/180/270 degrees clockwise. + +# [ 1, 2, 3 ] +# [ 4, 5, 6 ] +# [ 7, 8, 9 ] +# For example, if you rotate by 90 degrees then expected result should be like below + +# [ 7, 4, 1 ] +# [ 8, 5, 2 ] +# [ 9, 6, 3 ] + +my @aMatrix = ([1,2,3],[4,5,6],[7,8,9]); +print "Matrix\n"; +drawMatrix(\@aMatrix); + + +my @aMatrixRotated90 = rotateMatrix(\@aMatrix, 90); +print "Matrix rotated 90 degrees clockwise\n"; +drawMatrix(\@aMatrixRotated90); + +my @aMatrixRotated180 = rotateMatrix(\@aMatrix, 180); +print "Matrix rotated 180 degrees clockwise\n"; +drawMatrix(\@aMatrixRotated180); + +my @aMatrixRotated270 = rotateMatrix(\@aMatrix, 270); +print "Matrix rotated 270 degrees clockwise\n"; +drawMatrix(\@aMatrixRotated270); + +sub rotateMatrix { + my $refMatrix = shift; + my $degrees = shift; + my @aMatrixRotating =@{$refMatrix}; + while ( $degrees / 90 >= 1 ) { + @aMatrixRotating = rotateMatrix90Degrees(\@aMatrixRotating); + $degrees -= 90; + } + return @aMatrixRotating; +} + +sub rotateMatrix90Degrees { + my $refMatrix = shift; + my @aNewMatrix = (); + foreach my $x (0..$#{$refMatrix}){ + foreach my $y (0..$#{$$refMatrix[$x]}){ + $aNewMatrix[$y][$#aMatrix - $x] = $$refMatrix[$x][$y] ; + } + } + return @aNewMatrix; +} + +sub drawMatrix { + my $refMatrix = shift; + foreach my $row (@{$refMatrix}){ + print "[ ".join(', ',@{$row}). " ]\n"; + } +}
\ No newline at end of file diff --git a/challenge-053/alicia-bielsa/perl/ch-2.pl b/challenge-053/alicia-bielsa/perl/ch-2.pl new file mode 100644 index 0000000000..b357c91026 --- /dev/null +++ b/challenge-053/alicia-bielsa/perl/ch-2.pl @@ -0,0 +1,76 @@ +use strict; +use warnings; +use Data::Dumper; + +# Vowel Strings + +# Write a script to accept an integer 1 <= N <= 5 that would print all possible strings of size N formed by using only vowels (a, e, i, o, u). + +# The string should follow the following rules: + + # ‘a’ can only be followed by ‘e’ and ‘i’. + + # ‘e’ can only be followed by ‘i’. + + # ‘i’ can only be followed by ‘a’, ‘e’, ‘o’, and ‘u’. + + # ‘o’ can only be followed by ‘a’ and ‘u’. + + # ‘u’ can only be followed by ‘o’ and ‘e’. +#ae +#ai +#ei +#ia +#io +#iu +#ie +#oa +#ou +#uo +#ue + + + +my $size = $ARGV[0]; +unless (defined($size) && $size =~ /^[1-5]{1}$/ ) { + die "ERROR: Size must be between 1 and 5\n"; +} + + +my %hNodes = (); +$hNodes{'a'} = ['e','i']; +$hNodes{'e'} = ['i']; +$hNodes{'i'} = ['a', 'e', 'o', 'u']; +$hNodes{'o'} = ['a','u']; +$hNodes{'u'} = ['a','e']; + +my @aStrings = (); + + +foreach my $vowel ( keys(%hNodes)){ + addVowel($size, $vowel,''); +} + +sub addVowel { + my $currentLevel = shift; + my $currentVowel = shift; + my $currentString = shift; + $currentString .= $currentVowel; + if ($currentLevel == 1 ){ + push (@aStrings , $currentString); + return 0; + } else { + $currentLevel--; + } + + foreach my $nextVowel (@{$hNodes{$currentVowel}}){ + addVowel($currentLevel, $nextVowel,$currentString ); + } + + return 0; +} +foreach my $str (sort @aStrings){ + print "$str\n"; + +} + diff --git a/challenge-053/alicia-bielsa/raku/ch-1.p6 b/challenge-053/alicia-bielsa/raku/ch-1.p6 new file mode 100755 index 0000000000..8f26cfb444 --- /dev/null +++ b/challenge-053/alicia-bielsa/raku/ch-1.p6 @@ -0,0 +1,65 @@ +use v6; + + +# Rotate Matrix +# Write a script to rotate the followin matrix by given 90/180/270 degrees clockwise. + +# [ 1, 2, 3 ] +# [ 4, 5, 6 ] +# [ 7, 8, 9 ] +# For example, if you rotate by 90 degrees then expected result should be like below + +# [ 7, 4, 1 ] +# [ 8, 5, 2 ] +# [ 9, 6, 3 ] + +sub MAIN ( ) { + my @aMatrix = ([1,2,3],[4,5,6],[7,8,9]); + say "Matrix"; + drawMatrix( @aMatrix ); + + + my @aMatrixRotated90 = rotateMatrix(@aMatrix, 90); + say "Matrix rotated 90 degrees clockwise"; + drawMatrix(@aMatrixRotated90); + + + my @aMatrixRotated180 = rotateMatrix(@aMatrix, 180); + say "Matrix rotated 180 degrees clockwise"; + drawMatrix(@aMatrixRotated180); + + my @aMatrixRotated270 = rotateMatrix(@aMatrix, 270); + say "Matrix rotated 270 degrees clockwise"; + drawMatrix(@aMatrixRotated270); +} + +sub rotateMatrix ( @aMatrix , $degrees ) { + my @aMatrixRotating = @aMatrix; + my $degreesLeft = $degrees; + while ( $degreesLeft / 90 >= 1 ) { + @aMatrixRotating = rotateMatrix90Degrees(@aMatrixRotating ); + $degreesLeft -= 90; + } + return @aMatrixRotating; +} + + +sub rotateMatrix90Degrees ( @aMatrix ) { + my @aNewMatrix = () ; + for ( 0..@aMatrix.end ) -> $x { + for (0..@aMatrix[$x].end ) -> $y { + my $newY = @aMatrix.end - $x; + @aNewMatrix[$y][$newY] = @aMatrix[$x][$y] ; + } + } + return @aNewMatrix; +} + + +sub drawMatrix ( @aMatrix ) { + for @aMatrix -> @row { + say "[ " ~ @row.join(', ') ~ "]"; + } +} + + diff --git a/challenge-053/alicia-bielsa/raku/ch-2.p6 b/challenge-053/alicia-bielsa/raku/ch-2.p6 new file mode 100644 index 0000000000..d38b6f04c0 --- /dev/null +++ b/challenge-053/alicia-bielsa/raku/ch-2.p6 @@ -0,0 +1,64 @@ +use v6; + + +# Vowel Strings + +# Write a script to accept an integer 1 <= N <= 5 that would print all possible strings of size N formed by using only vowels (a, e, i, o, u). + +# The string should follow the following rules: + + # ‘a’ can only be followed by ‘e’ and ‘i’. + + # ‘e’ can only be followed by ‘i’. + + # ‘i’ can only be followed by ‘a’, ‘e’, ‘o’, and ‘u’. + + # ‘o’ can only be followed by ‘a’ and ‘u’. + + # ‘u’ can only be followed by ‘o’ and ‘e’. +#ae +#ai +#ei +#ia +#io +#iu +#ie +#oa +#ou +#uo +#ue + + +my @aStrings = (); + my %hNodes = (); +%hNodes{'a'} = ['e','i']; +%hNodes{'e'} = ['i']; +%hNodes{'i'} = ['a', 'e', 'o', 'u']; +%hNodes{'o'} = ['a','u']; +%hNodes{'u'} = ['a','e']; + +sub MAIN ( Int $size ) { + unless (defined($size) && $size >= 1 && $size <= 5 ) { + die "ERROR: Size must be between 1 and 5\n"; + } + for keys(%hNodes) -> $vowel { + addVowel($size, $vowel,''); + } + @aStrings.sort.map({ say $_ }); + +} + +sub addVowel ($currentLevel, $currentVowel , $currentString) { + + my $addToString = $currentString ~ $currentVowel; + my $newLevel = $currentLevel -1; + if $currentLevel == 1 { + @aStrings.push($addToString ); + return 0; + } + for @(%hNodes{$currentVowel}) -> $nextVowel { + addVowel($newLevel, $nextVowel,$addToString ); + } + return 0; +} + |
