diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-02-01 18:53:12 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-01 18:53:12 +0000 |
| commit | d595b23cf78bb42a2fc3daec973b8f8e2db77993 (patch) | |
| tree | b2730522cef1f5f1e9e45ecf6984219a5477d9ef /challenge-045 | |
| parent | 8054d5bdeb4bfe1cfb524e29b8f31edcb3da66f8 (diff) | |
| parent | 5eebfb32354613c91002b6d140f6225955dd476e (diff) | |
| download | perlweeklychallenge-club-d595b23cf78bb42a2fc3daec973b8f8e2db77993.tar.gz perlweeklychallenge-club-d595b23cf78bb42a2fc3daec973b8f8e2db77993.tar.bz2 perlweeklychallenge-club-d595b23cf78bb42a2fc3daec973b8f8e2db77993.zip | |
Merge pull request #1194 from saiftynet/branch-045
Welcome back Mo!
Diffstat (limited to 'challenge-045')
| -rw-r--r-- | challenge-045/saiftynet/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-045/saiftynet/perl/ch-2.pl | 9 |
2 files changed, 41 insertions, 0 deletions
diff --git a/challenge-045/saiftynet/perl/ch-1.pl b/challenge-045/saiftynet/perl/ch-1.pl new file mode 100644 index 0000000000..d5d4983df1 --- /dev/null +++ b/challenge-045/saiftynet/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/env/perl +# Challenge 046 Task #1 +# The square secret code mechanism first removes any space from the original message. +# Then it lays down the message in a row of 8 columns. The coded message is then +# obtained by reading down the columns going left to right. +# Write a script that accepts a message from command line and prints the equivalent coded message. + +# This extends the task by allowing any number of columns, with default of 8 + +my $stringToEncode=$ARGV[0]//"The quick brown fox jumps over the lazy dog"; +my $cols=$ARGV[1]//8; + +print pivotEncode($stringToEncode,$cols),"\n"; + +# Sub routine pivotEncode; Takes a string to encode and an optional +# number of columns (default 8); returns encoded string + +sub pivotEncode{ + my $str=shift; + my $cols=shift//8; + $str=~s/\s//gm; # remove spaces + @splitChars=($str=~/(.{$cols}|.+)/g); # split into blocks + my $result; # initialise result + foreach my $index (0..$cols-1) { # now select character + foreach my $row ( @splitChars ){ # in each block and + # append it to result + $result.= substr($row,$index,1) if ($index<length $row) + } + $result.=" "; #intersperse spaces + } + return $result; # return encrypted +} diff --git a/challenge-045/saiftynet/perl/ch-2.pl b/challenge-045/saiftynet/perl/ch-2.pl new file mode 100644 index 0000000000..111a26f05d --- /dev/null +++ b/challenge-045/saiftynet/perl/ch-2.pl @@ -0,0 +1,9 @@ +#perl 5.22.1 +# Challenge 045 Task 2 +# Write a script that dumps its own source code. For example, say, +# the script name is ch-2.pl then the following command should +# return nothing. +# $ perl ch-2.pl | diff - ch-2.pl + +open (my $fh, '<:encoding(UTF-8)', $0 ) ; +print while(<$fh>); |
