Task 1: Square Secret Code
Given a message, remove spaces, convert to lowercase, represent it in an 8-column grid, and encode the message by reading column by column from left to right.
Example Output
Input: "The quick brown fox jumps over the lazy dog"
Output: tbjrd hruto eomhg qwpe unsl ifoa covz kxey
Logic
Compute the number of rows based on message length and 8 columns. Read characters at intervals of 8 to extract characters column-wise, separating words by spaces.
Perl Solution
ch-1.pl
sub square_secret_code {
my $message = shift;
my $cols = 8;
$message =~ s/\s+//g;
$message = lc($message);
my $rows = int(length($message) / $cols) + 1;
my $coded_message = "";
for (my $i = 0; $i < $cols; $i++) {
for (my $j = 0; $j < $rows; $j++) {
my $index = $j * $cols + $i;
if ($index < length($message)) {
$coded_message .= substr($message, $index, 1);
}
}
$coded_message .= " " if $i < $cols - 1;
}
return $coded_message;
}
Python Solution
ch-1.py
def square_secret_code(message: str) -> str:
message = message.lower().replace(" ", "")
cols = 8
rows = (len(message) + cols - 1) // cols
coded_message = ""
for i in range(cols):
for j in range(rows):
index = j * cols + i
if index < len(message):
coded_message += message[index]
coded_message += " " if i < cols - 1 else ""
return coded_message
Task 2: Source Code / Quine
Write a script that when executed prints its own source code without using external system commands.
Logic
Open the current script file using the special variable representing the script's path (e.g. $0 in Perl or sys.argv[0] in Python) and stream it directly to standard output.
Perl Solution
ch-2.pl
my $script_name = $0;
open(my $fh, '<', $script_name) or die "Could not open file '$script_name' $!";
while (my $line = <$fh>) {
print $line;
}
close($fh);
Python Solution
ch-2.py
def print_own_source_code() -> None:
script_name = sys.argv[0]
with open(script_name, 'r') as f:
for line in f:
print(line, end='')