rinodrops icon

CodeIQ: Space Talky Problem Solver, 1.1 Revision 2.

rinodrops | PRO | 04/28/14 12:57:22 PM UTC | 0 ⭐ | 376 👁️ | Never ⏰ | []
Perl |

1.05 KB

|

None

|

0 👍

/

0 👎

#! /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 = <FIN>) {
    # 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

Comments