diff options
| author | saiftynet <saiftynet@gmail.com> | 2020-03-25 19:44:24 +0000 |
|---|---|---|
| committer | saiftynet <saiftynet@gmail.com> | 2020-03-25 19:44:24 +0000 |
| commit | f50fb86e188b017bf27ca087b0265899fae3898a (patch) | |
| tree | 64458a89f793cb4ede202a10a735ff6aca74aaaf /challenge-053/saiftynet | |
| parent | 76149df6dc425f24fecdb8679bed53fedc00c29b (diff) | |
| download | perlweeklychallenge-club-f50fb86e188b017bf27ca087b0265899fae3898a.tar.gz perlweeklychallenge-club-f50fb86e188b017bf27ca087b0265899fae3898a.tar.bz2 perlweeklychallenge-club-f50fb86e188b017bf27ca087b0265899fae3898a.zip | |
Challenge-053 solutions by saiftynet
Diffstat (limited to 'challenge-053/saiftynet')
| -rw-r--r-- | challenge-053/saiftynet/perl/ch-1.pl | 78 | ||||
| -rw-r--r-- | challenge-053/saiftynet/perl/ch-2.pl | 48 |
2 files changed, 126 insertions, 0 deletions
diff --git a/challenge-053/saiftynet/perl/ch-1.pl b/challenge-053/saiftynet/perl/ch-1.pl new file mode 100644 index 0000000000..f6fa594ed2 --- /dev/null +++ b/challenge-053/saiftynet/perl/ch-1.pl @@ -0,0 +1,78 @@ +#!/usr/env/perl +# Task 1 Challenge 053 Solution by saiftynet +# 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 ] + +# This script allows 7 different rotations and flips +# 90 (or "cw"...clockwise), 180 (or "ht" ...half turn), +# 270 (or "ccw"...counter clockwise), 0 returns original matrix. +# It also understands flips "v"ertical and "h"orizontal, +# and diagonal "lr" (flip on topleft to bottomright diagonal) +# and "rl" (flip on topright to bottomleft diagonal). +# flip() can work with 2d square or rectangular matrices +# of any size. of course the matrix does not require just +# numeric contents. + +# square matrix made of alphanumeric characters +my $matrix1=[[qw{1 2 3 4 5}], + [qw{6 7 8 9 0}], + [qw{A B C D E}], + [qw{F G H I J}], + [qw{K L M N O}]]; + +# unicode characters making a rectangular matrix +my $matrix2=[[qw{- - - █ - -}], + [qw{- - - ░ █ -}], + [qw{█ █ █ ░ ░ █}], + [qw{- - - ░ █ -}], + [qw{- - - █ - -}]]; + +foreach (qw/0 cw ht ccw h v lr rl/){ + print "\nRotating/flipping $_\n"; + printMatrix (flip($matrix1,$_)," "); +} + +foreach (0,90,180,270){ + print "\nRotating by $_\n"; + printMatrix (flip($matrix2,$_ )); +} + +sub flip{ + my $arr=shift; + my $direction=shift; + my @flipped=(); + return $arr unless $direction ; # return original list for 0 deg trun + if ($direction=~/^cw|ccw|lr|rl|90|270$/){ + foreach my $row(0..scalar @{$$arr[0]}-1){ # for each row + $flipped[$row]=[]; # initialise empty row + foreach my $col(0..scalar @$arr-1){ # for each column + push @{$flipped[$row]}, $$arr[$col][$row]; # this does a diagonal flip + }; + @{$flipped[$row]}=reverse @{$flipped[$row]} if $direction =~/^cw|90|rl$/; + } + @flipped=reverse @flipped if $direction =~/^ccw|270|rl$/; + } + elsif ($direction eq 'v'){ + @flipped=reverse (@$arr) + } + elsif ($direction eq 'h'){ + my $row=0; + foreach (@$arr){ + @{$flipped[$row++]}= reverse @$_; + }; + } + elsif ($direction =~ /^180|ht$/){ + @flipped=@{flip(flip($arr,"v"),"h")}; + } + return \@flipped; +} + +sub printMatrix{ # prints out a matrix with an appropriate separator + my $matrix=shift; + my $separator=shift // ""; + foreach my $r (@$matrix){ print "",(join $separator,@$r),"\n" }; +} diff --git a/challenge-053/saiftynet/perl/ch-2.pl b/challenge-053/saiftynet/perl/ch-2.pl new file mode 100644 index 0000000000..dfda6141f5 --- /dev/null +++ b/challenge-053/saiftynet/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/env/perl +# Task 2 Challenge 053 Solution by saiftynet +# 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’. +# For example, if the given integer N = 2 then script should print +# the following strings: +# ae ai ei ia io iu ie oa ou uo ue + +# This script takes an integer either as a command line parameter, or +# if no parameters offered, requests the integer from the user. It then +# displays all the vowel strings with that many characters + +my $target=$ARGV[0]; # command line parameters +print "Enter the string length >> " # Prompts for input + and chomp($target=<>) unless $target; # if parameter not supplied + +print join (" ",vowelStrings($target)); # display returned list + +sub vowelStrings{ + my $target=shift; + my %following=( # hash containing lists of valid following vowels + a =>['e','i'], + e =>['i'], + i =>['a', 'e', 'o', 'u'], + o =>['a', 'u'], + u =>['o','e'], + ); + my @list=(qw{a e i o u }); # start with list of vowels + +# treats @list as a circular list shifting a string from one end, +# create a list made by adding all the potential following vowels +# to that string, and pushing these to the end, continuing until +# $target length achieved + + while(length $list[0] <$target){ + my $str= shift @list; + push @list, map {$str.$_} @{$following{substr($str,-1,1)}}; + } + return @list, +} |
