aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-01-31 17:52:11 +0000
committerGitHub <noreply@github.com>2020-01-31 17:52:11 +0000
commitb09d940dfc23db41301a6f8bbaab1a17085d49ea (patch)
tree9e6746ec01bb706c12feddcdba1ded1663433726
parent18588759ed281749a37e0df411456cc5bd310532 (diff)
parent38d5237b1871d44988a802b9edffca0685189c0c (diff)
downloadperlweeklychallenge-club-b09d940dfc23db41301a6f8bbaab1a17085d49ea.tar.gz
perlweeklychallenge-club-b09d940dfc23db41301a6f8bbaab1a17085d49ea.tar.bz2
perlweeklychallenge-club-b09d940dfc23db41301a6f8bbaab1a17085d49ea.zip
Merge pull request #1188 from andrezgz/challenge-045
challenge-045 andrezgz solution
-rw-r--r--challenge-045/andrezgz/perl/ch-1.pl44
-rw-r--r--challenge-045/andrezgz/perl/ch-2.pl92
2 files changed, 136 insertions, 0 deletions
diff --git a/challenge-045/andrezgz/perl/ch-1.pl b/challenge-045/andrezgz/perl/ch-1.pl
new file mode 100644
index 0000000000..774899857d
--- /dev/null
+++ b/challenge-045/andrezgz/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/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 equivalent coded message.
+
+use strict;
+use warnings;
+
+# only lower-case letters from arguments form a string
+my $msg = join '',
+ map {my $w = lc $_; $w =~ s/[[:^lower:]]//g; $w }
+ @ARGV or die "USAGE: $0 <message>";
+
+my %cols;
+# each letter is appended to the corresponding column
+$cols{$_ % 8} .= substr $msg, $_, 1 for (0 .. (length $msg) -1 );
+
+# coded message is formed by printing each column string in order
+print join ' ', map { $cols{$_} } sort keys %cols;
+
+__END__
+
+./ch-1.pl The quick brown fox jumps over the lazy dog
+tbjrd hruto eomhg qwpe unsl ifoa covz kxey
diff --git a/challenge-045/andrezgz/perl/ch-2.pl b/challenge-045/andrezgz/perl/ch-2.pl
new file mode 100644
index 0000000000..f25d91cf58
--- /dev/null
+++ b/challenge-045/andrezgz/perl/ch-2.pl
@@ -0,0 +1,92 @@
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-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 strict;
+use warnings;
+use v5.10;
+
+# This is my quine!
+
+my @s = (
+q&&,
+q&say <<'EOT';&,
+q&#!/usr/bin/perl&,
+q&&,
+q&# https://perlweeklychallenge.org/blog/perl-weekly-challenge-045/&,
+q&# Task #2&,
+q&# Source Dumper&,
+q&# Write a script that dumps its own source code.&,
+q&# For example, say, the script name is ch-2.pl then the following command should returns nothing.&,
+q&#&,
+q&# $ perl ch-2.pl | diff - ch-2.pl&,
+q&&,
+q&use strict;&,
+q&use warnings;&,
+q&use v5.10;&,
+q&&,
+q&# This is my quine!&,
+q&EOT&,
+q&&,
+q&say 'my @s = (';&,
+q&foreach my $line (@s) {&,
+q& say 'q'.chr(38).$line.chr(38).','&,
+q&}&,
+q&say ');';&,
+q&&,
+q&foreach my $line (@s) {&,
+q& say $line&,
+q&}&,
+q&&,
+q&say <<'EOT';&,
+q&__END__&,
+q&&,
+q&./ch-2.pl | diff - ch-2.pl&,
+q&EOT&,
+q&&,
+);
+
+say <<'EOT';
+#!/usr/bin/perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-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 strict;
+use warnings;
+use v5.10;
+
+# This is my quine!
+EOT
+
+say 'my @s = (';
+foreach my $line (@s) {
+ say 'q'.chr(38).$line.chr(38).','
+}
+say ');';
+
+foreach my $line (@s) {
+ say $line
+}
+
+say <<'EOT';
+__END__
+
+./ch-2.pl | diff - ch-2.pl
+EOT
+
+__END__
+
+./ch-2.pl | diff - ch-2.pl
+