diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-01-30 12:09:18 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-30 12:09:18 +0000 |
| commit | d0ed5cd222b1e18ccd223335532e22e665c52479 (patch) | |
| tree | 25729b999b33abde829f365aebdffaa77f9e4868 | |
| parent | 02067f1b5c4e29958ed6d8c23bee4901b3cddfa5 (diff) | |
| parent | 4edf958a4fe505cdecdc8e1c900fc3c88ad6e68e (diff) | |
| download | perlweeklychallenge-club-d0ed5cd222b1e18ccd223335532e22e665c52479.tar.gz perlweeklychallenge-club-d0ed5cd222b1e18ccd223335532e22e665c52479.tar.bz2 perlweeklychallenge-club-d0ed5cd222b1e18ccd223335532e22e665c52479.zip | |
Merge pull request #1183 from ndelucca/challenge-045/ndelucca
challenge045-ndelucca
| -rw-r--r-- | challenge-045/ndelucca/perl5/ch-1.pl | 67 | ||||
| -rw-r--r-- | challenge-045/ndelucca/perl5/ch-2.pl | 22 |
2 files changed, 89 insertions, 0 deletions
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 +# |
