aboutsummaryrefslogtreecommitdiff
path: root/challenge-045
diff options
context:
space:
mode:
authorPerlMonk Athanasius <PerlMonk.Athanasius@gmail.com>2020-01-31 23:27:30 -0800
committerPerlMonk Athanasius <PerlMonk.Athanasius@gmail.com>2020-01-31 23:27:30 -0800
commitf02b8c4d177a0e434ea5737c66a96122b79247ca (patch)
treeda5085da6c8165ce12f290d5428359296a0402f7 /challenge-045
parentd094e986802beac5ebd557008a31d8a8032093b5 (diff)
downloadperlweeklychallenge-club-f02b8c4d177a0e434ea5737c66a96122b79247ca.tar.gz
perlweeklychallenge-club-f02b8c4d177a0e434ea5737c66a96122b79247ca.tar.bz2
perlweeklychallenge-club-f02b8c4d177a0e434ea5737c66a96122b79247ca.zip
Perl solution to Task #1 ("Square Secret Code") of the Perl Weekly Challenge #045
On branch branch-for-challenge-045 Changes to be committed: new file: challenge-045/athanasius/perl/ch-1.pl
Diffstat (limited to 'challenge-045')
-rw-r--r--challenge-045/athanasius/perl/ch-1.pl108
1 files changed, 108 insertions, 0 deletions
diff --git a/challenge-045/athanasius/perl/ch-1.pl b/challenge-045/athanasius/perl/ch-1.pl
new file mode 100644
index 0000000000..da06db6d17
--- /dev/null
+++ b/challenge-045/athanasius/perl/ch-1.pl
@@ -0,0 +1,108 @@
+#!perl
+
+################################################################################
+=comment
+
+Perl Weekly Challenge 045
+=========================
+
+Task #1
+-------
+*Square Secret Code*
+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.
+
+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 code 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 equiva-
+lent coded message.
+
+=cut
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2020 PerlMonk Athanasius #
+#--------------------------------------#
+
+use strict;
+use warnings;
+use Const::Fast;
+
+const my $COLUMNS => 8;
+
+BEGIN
+{
+ $| = 1;
+ print "\n";
+}
+
+#===============================================================================
+MAIN:
+#===============================================================================
+{
+ @ARGV > 0 or die "Missing command-line message\n";
+
+ my $plain = join '', @ARGV;
+ my $encoded = encode($plain);
+ my $decoded = decode($encoded);
+
+ print "Original message:\n>$plain<\n";
+ print "\nEncoded message:\n>$encoded<\n";
+ print "\nDecoded message:\n>$decoded<\n";
+
+ $decoded eq $plain or die "\nCoding error\n";
+}
+
+#-------------------------------------------------------------------------------
+sub encode
+#-------------------------------------------------------------------------------
+{
+ my ($plain) = @_;
+ my @rows;
+ push @rows, substr($plain, 0, $COLUMNS, '') while $plain;
+ my $encoded = '';
+
+ for my $col (0 .. $COLUMNS - 1)
+ {
+ $encoded .= ' ' if $encoded;
+
+ for my $row (0 .. $#rows)
+ {
+ my $text = $rows[$row];
+ $encoded .= substr($text, $col, 1) if $col < length $text;
+ }
+ }
+
+ return $encoded;
+}
+
+#-------------------------------------------------------------------------------
+sub decode
+#-------------------------------------------------------------------------------
+{
+ my ($encoded) = @_;
+ my @rows = split /\s+/, $encoded;
+ my $decoded = '';
+
+ for my $col (0 .. length($rows[0]) - 1)
+ {
+ $decoded .= substr($rows[$_], $col, 1) for 0 .. $#rows;
+ }
+
+ return $decoded;
+}
+
+################################################################################