ClarkeRubber

UNSW ProgComp: Problem 3 - 2008

Jun 12th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.79 KB | None | 0 0
  1. <?php
  2. //word ladder checker
  3.  
  4. $input = <<<END
  5. 8
  6. 14 frown crown croon crook crock chock shock shack stack stick stalk stale stile smile
  7. 7 brown grown groan groat great greet green
  8. 16 great greet greed breed bread bred bleed blend bland blank black slack stack stalk stall small
  9. 2 hero zero
  10. 11 cain fain fail fall fill full fell fill file aile able
  11. 7 o i c u r m t
  12. 4 batt bait bai1 ba11
  13. 7 less loss lose lost love move more
  14. END;
  15.  
  16. $input = explode("\n", $input);
  17. array_shift($input);
  18. $letters = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
  19.  
  20. foreach($input as $key => $value){
  21.     $temp = explode(" ", $value);
  22.     array_shift($temp);
  23.     foreach($temp as $key3 => $value3){
  24.         if($key3 == count($temp)-1){
  25.             echo "$value3\n  ";
  26.         }else{
  27.             echo "$value3 ";
  28.         }
  29.     }
  30.     $history = array();
  31.     foreach($temp as $key2 => $value2){
  32.         if($key2 == 0){
  33.             $length = strlen($value2);
  34.             $history[] = $value2;
  35.         }else{
  36.             if($value2 != strtolower($value2)){
  37.                 //string is not all lower case
  38.                 echo "contains other than lower-case letters \"$value2\"\n";
  39.                 break;
  40.             }
  41.             if($length != strlen($value2)){
  42.                 //string is on inconsistent length
  43.                 echo "Inconsistent word length for \"$value2\"\n";
  44.                 break;
  45.             }
  46.             if(levenshtein($value2, $history[$key2-1]) != 1){
  47.                 //more than 1 changes
  48.                 echo "Too many changes to produce \"$value2\"\n";
  49.                 break;
  50.             }
  51.             if(in_array($value2, $history)){
  52.                 //string is already in the array
  53.                 echo "Repeated word \"$value2\"\n";
  54.                 break;
  55.             }
  56.             if(str_replace($letters, '', $value2) != ''){
  57.                 //string contains non-letters
  58.                 echo "Non-letters in \"$value2\"\n";
  59.                 break;
  60.             }
  61.             $history[] = $value2;
  62.         }
  63.         if($key2 == count($temp)-1){
  64.             echo "Feasible\n";
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment