From 7f42bbdfd21934f517a97f24a71b3ca06f5362a9 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 7 Apr 2022 10:58:20 +0100 Subject: Add Perl solution to challenge 045 --- challenge-045/paulo-custodio/Makefile | 3 ++ challenge-045/paulo-custodio/README | 1 + challenge-045/paulo-custodio/perl/ch-1.pl | 52 ++++++++++++++++++++++++++++++ challenge-045/paulo-custodio/perl/ch-2.pl | 15 +++++++++ challenge-045/paulo-custodio/t/test-1.yaml | 5 +++ 5 files changed, 76 insertions(+) create mode 100644 challenge-045/paulo-custodio/Makefile create mode 100644 challenge-045/paulo-custodio/README create mode 100644 challenge-045/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-045/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-045/paulo-custodio/t/test-1.yaml 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 -- cgit