aboutsummaryrefslogtreecommitdiff
path: root/challenge-045
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-01-30 13:16:02 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-01-30 13:16:02 +0000
commitfcc1f0519c6131b86c68eb479f76e1dac99e67ae (patch)
treeecf471ef890750e090170ca19beaaf939b6b1dfe /challenge-045
parentf7ad50f67be9ab73486a5e67bc616fa2fdc215c5 (diff)
downloadperlweeklychallenge-club-fcc1f0519c6131b86c68eb479f76e1dac99e67ae.tar.gz
perlweeklychallenge-club-fcc1f0519c6131b86c68eb479f76e1dac99e67ae.tar.bz2
perlweeklychallenge-club-fcc1f0519c6131b86c68eb479f76e1dac99e67ae.zip
- Added solutions by Wanderdoc.
Diffstat (limited to 'challenge-045')
-rw-r--r--challenge-045/wanderdoc/perl/ch-1.pl64
-rw-r--r--challenge-045/wanderdoc/perl/ch-2.pl13
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-045/wanderdoc/perl/ch-1.pl b/challenge-045/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..01c1cb109f
--- /dev/null
+++ b/challenge-045/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,64 @@
+#!perl
+
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+
+The squate 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.
+
+For example, the message is "The quick brown fox jumps over the lazy dog".
+Then the message would be laid out as below:
+
+thequick
+brownfox
+jumpsove
+rthelazy
+dog
+
+The coded message would be as below:
+
+tbjrd hruto eomhg qwpe unsl ifoa covz kxey
+
+Write a script that accepts a message from command line and prints the equivalent coded message.
+
+=cut
+
+use Const::Fast; # To use the constant in the regex.
+
+const my $SECRET => 8;
+const my $REGEX => qr/(.{1,${SECRET}})/;
+
+my $message = "@ARGV" || "The quick brown fox jumps over the lazy dog";
+
+sub encoding_message
+{
+ my $str = $_[0];
+ $str =~ tr/ //ds;
+ $str = lc $str;
+
+ my @rows = map [split(//,$_)], ($str =~ /$REGEX/g);
+
+ my @coded = map { my $idx = $_;
+ my @slice = map $_->[$idx] // '', @rows; [@slice];
+ } 0 .. $SECRET - 1;
+
+ my $enc = join(' ', map join('',@$_), @coded);
+
+ return $enc;
+}
+
+print encoding_message($message), $/;
+
+sub decoding_message
+{
+ my $str = $_[0];
+ my @words = map [split(//,$_)], split(' ', $str);
+ my @txt = map { my $idx = $_;
+ my @slice = map $_->[$idx] // '', @words; [@slice];
+ } 0 .. $#words;
+
+ my $dec = join('', map join('',@$_), @txt);
+}
+
+print decoding_message(encoding_message($message)), $/;
diff --git a/challenge-045/wanderdoc/perl/ch-2.pl b/challenge-045/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..f544747ad7
--- /dev/null
+++ b/challenge-045/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,13 @@
+#!perl
+
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+
+Write a script that dumps its own source code. For example, say, the script name is ch-2.pl then the following command should returns nothing. perl ch-2.pl | diff - ch-2.pl
+
+=cut
+
+open my $in, "<", $0 or die "$!";
+for ( <$in> ) {print $_;}