rinodrops

CodeIQ: Space Talky Problem Solver, 1.1 Revision 2.

Apr 28th, 2014
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.05 KB | None | 0 0
  1. #! /usr/bin/env perl
  2.  
  3. # CodeIQ: Space Talky Problem Solver, 1.1 Revision 2.
  4.  
  5. use 5.12.0;
  6. use warnings;
  7.  
  8. # open words file; words.txt must be saved with 'ff=unix'
  9. open(FIN, "words.txt") or die "file couldn't be opened\n";
  10. while (my $word = <FIN>) {
  11.     # drop LF
  12.     chomp($word);
  13.  
  14.     my $word_size = length($word);  # length of the word
  15.     my $input = '';         # predicted input
  16.     my $c_last = '';        # the last letter
  17.  
  18.     # size is odd; cannot generated by the encoder
  19.     if ($word_size % 2) {
  20.         $input = 'X';
  21.     }
  22.     # could be generatable
  23.     else {
  24.         for (my $i = 0; $i < $word_size; $i+=2) {
  25.             # obtain a letter and its length
  26.             my $c = substr($word, $i, 1);
  27.             my $n = unpack("C", substr($word, $i+1, 1)) - 0x61 + 1;
  28.  
  29.             if ($c ne $c_last) {
  30.                 $input .= $c x $n;
  31.             }
  32.             else {
  33.                 $input = 'X';
  34.                 last;
  35.             }
  36.  
  37.             # store the last letter
  38.             $c_last = $c;
  39.  
  40.             # ...however, if its length is 26, clear the last letter (squeezed!)
  41.             $c_last = '' if ($n eq 26);
  42.         }
  43.     }
  44.     print "$input:$word\n";
  45.  
  46. }
  47. close(FIN);
  48.  
  49. print <<"END";
  50. ENV: Perl
  51. POINT: X
  52. END
Advertisement
Add Comment
Please, Sign In to add comment