From 4edf958a4fe505cdecdc8e1c900fc3c88ad6e68e Mon Sep 17 00:00:00 2001 From: ndelucca Date: Thu, 30 Jan 2020 00:00:11 -0300 Subject: challenge045-ndelucca --- challenge-045/ndelucca/perl5/ch-1.pl | 67 ++++++++++++++++++++++++++++++++++++ challenge-045/ndelucca/perl5/ch-2.pl | 22 ++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 challenge-045/ndelucca/perl5/ch-1.pl create mode 100644 challenge-045/ndelucca/perl5/ch-2.pl diff --git a/challenge-045/ndelucca/perl5/ch-1.pl b/challenge-045/ndelucca/perl5/ch-1.pl new file mode 100644 index 0000000000..2ff5da8876 --- /dev/null +++ b/challenge-045/ndelucca/perl5/ch-1.pl @@ -0,0 +1,67 @@ +#!/usr/bin/perl + +# Square Secret Code + +# 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 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; + +die "You must insert a message to encode.\n" unless @ARGV; + +my $message = shift; +my $columns = shift || 8; + +my @matrix = (); +my $code = ''; + +# We clean up the message a bit +$message =~ s/\s+//g; + +# Then we must turn the message into an array of arrays +my @rows = unpack "(A$columns)*", lc $message; + +foreach my $word ( @rows ){ + my @chars = split //, $word; + push @matrix, \@chars; +} + +# Now we simply print it in the order we want +for (0..$columns){ + + for my $row( @matrix ){ + $code .= shift @$row || ''; + } + + $code .= " "; +} + +print "$code\n"; + +# perl ch-1.pl "The quick brown fox jumps over the lazy dog" +# tbjrd hruto eomhg qwpe unsl ifoa covz kxey + +# perl ch-1.pl "The quick brown fox jumps over the lazy dog" 15 +# txz hjy eud qmo upg is co kv be rr ot wh ne fl oa + +# perl ch-1.pl "Maybe this code could be better?" +# msle acdr yob? bde eeb tce hot iut + diff --git a/challenge-045/ndelucca/perl5/ch-2.pl b/challenge-045/ndelucca/perl5/ch-2.pl new file mode 100644 index 0000000000..700805b210 --- /dev/null +++ b/challenge-045/ndelucca/perl5/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +# 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; + +# We can add whatever we want, anywhere + +open(my $fh, "<", __FILE__) || die "Couldn't open $0 for reading because: $! "; + +print <$fh>; + +close($fh); + +# perl ch-2.pl | diff - ch-2.pl +# -- cgit