#! /usr/bin/env perl # CodeIQ: Space Talky Problem Solver, 1.1 Revision 2. use 5.12.0; use warnings; # open words file; words.txt must be saved with 'ff=unix' open(FIN, "words.txt") or die "file couldn't be opened\n"; while (my $word = ) { # drop LF chomp($word); my $word_size = length($word); # length of the word my $input = ''; # predicted input my $c_last = ''; # the last letter # size is odd; cannot generated by the encoder if ($word_size % 2) { $input = 'X'; } # could be generatable else { for (my $i = 0; $i < $word_size; $i+=2) { # obtain a letter and its length my $c = substr($word, $i, 1); my $n = unpack("C", substr($word, $i+1, 1)) - 0x61 + 1; if ($c ne $c_last) { $input .= $c x $n; } else { $input = 'X'; last; } # store the last letter $c_last = $c; # ...however, if its length is 26, clear the last letter (squeezed!) $c_last = '' if ($n eq 26); } } print "$input:$word\n"; } close(FIN); print <<"END"; ENV: Perl POINT: X END