Created
February 2, 2020 23:48
-
-
Save adamcrussell/b67df3b76c00d4b37fb85b033b707434 to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 045
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
## | |
# Write a script that accepts a message from the | |
# command line and prints the equivalent square secret | |
# coded message. | |
## | |
use constant SQUARE_SIZE => 8; | |
sub encode{ | |
my($message) = @_; | |
$message =~ tr/ //d; | |
my $encoded; | |
my @buckets; | |
my @characters = split(//, lc($message)); | |
for my $i (0 .. @characters){ | |
$buckets[$i % SQUARE_SIZE] = [] if !$buckets[$i % SQUARE_SIZE]; | |
push @{$buckets[$i % SQUARE_SIZE]}, $characters[$i] if $characters[$i]; | |
} | |
for my $bucket (@buckets){ | |
$encoded .= join("", @{$bucket}) . " "; | |
} | |
return $encoded; | |
} | |
MAIN:{ | |
my $message = $ARGV[0]; | |
print encode($message) . "\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print<<''x2,"\n" | |
print<<''x2,"\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment