aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2022-04-07 10:58:20 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2022-04-07 10:58:20 +0100
commit7f42bbdfd21934f517a97f24a71b3ca06f5362a9 (patch)
treea724a05405c000ecc5fecde39a798ab3419056ae
parent311f215cc207e697483b4200aafa33774a90972e (diff)
downloadperlweeklychallenge-club-7f42bbdfd21934f517a97f24a71b3ca06f5362a9.tar.gz
perlweeklychallenge-club-7f42bbdfd21934f517a97f24a71b3ca06f5362a9.tar.bz2
perlweeklychallenge-club-7f42bbdfd21934f517a97f24a71b3ca06f5362a9.zip
Add Perl solution to challenge 045
-rw-r--r--challenge-045/paulo-custodio/Makefile3
-rw-r--r--challenge-045/paulo-custodio/README1
-rw-r--r--challenge-045/paulo-custodio/perl/ch-1.pl52
-rw-r--r--challenge-045/paulo-custodio/perl/ch-2.pl15
-rw-r--r--challenge-045/paulo-custodio/t/test-1.yaml5
5 files changed, 76 insertions, 0 deletions
diff --git a/challenge-045/paulo-custodio/Makefile b/challenge-045/paulo-custodio/Makefile
new file mode 100644
index 0000000000..6a9bfeacd6
--- /dev/null
+++ b/challenge-045/paulo-custodio/Makefile
@@ -0,0 +1,3 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
+ perl ./perl/ch-2.pl | diff - ./perl/ch-2.pl \ No newline at end of file
diff --git a/challenge-045/paulo-custodio/README b/challenge-045/paulo-custodio/README
new file mode 100644
index 0000000000..87dc0b2fbd
--- /dev/null
+++ b/challenge-045/paulo-custodio/README
@@ -0,0 +1 @@
+Solution by Paulo Custodio
diff --git a/challenge-045/paulo-custodio/perl/ch-1.pl b/challenge-045/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..a7ae828b0e
--- /dev/null
+++ b/challenge-045/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,52 @@
+#!/usr/bin/env perl
+
+# 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
+# equivalent coded message.
+
+use Modern::Perl;
+
+say encode("@ARGV");
+
+sub encode {
+ my($mess) = @_;
+ $mess = lc($mess);
+ $mess =~ s/\W//g;
+
+ my @box;
+ while ($mess ne '') {
+ push @box, substr($mess,0,8);
+ $mess = length($mess)>=8 ? substr($mess, 8) : '';
+ }
+
+ my $encoded = '';
+ for my $col (0..7) {
+ $encoded .= ' ' if $encoded ne '';
+ for my $row (0 .. $#box) {
+ if (length($box[$row]) >= $col) {
+ $encoded .= substr($box[$row], $col, 1);
+ }
+ }
+ }
+
+ return $encoded;
+}
diff --git a/challenge-045/paulo-custodio/perl/ch-2.pl b/challenge-045/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..f92859885f
--- /dev/null
+++ b/challenge-045/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,15 @@
+#!/usr/bin/env perl
+
+# Challenge 045
+#
+# TASK #2
+# Source Dumper
+# 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
+
+use Modern::Perl;
+
+@ARGV = ($0);
+print <>;
diff --git a/challenge-045/paulo-custodio/t/test-1.yaml b/challenge-045/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..993708c0b3
--- /dev/null
+++ b/challenge-045/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: the quick brown fox jumps over the lazy dog
+ input:
+ output: tbjrd hruto eomhg qwpe unsl ifoa covz kxey