Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //word ladder checker
- $input = <<<END
- 8
- 14 frown crown croon crook crock chock shock shack stack stick stalk stale stile smile
- 7 brown grown groan groat great greet green
- 16 great greet greed breed bread bred bleed blend bland blank black slack stack stalk stall small
- 2 hero zero
- 11 cain fain fail fall fill full fell fill file aile able
- 7 o i c u r m t
- 4 batt bait bai1 ba11
- 7 less loss lose lost love move more
- END;
- $input = explode("\n", $input);
- array_shift($input);
- $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');
- foreach($input as $key => $value){
- $temp = explode(" ", $value);
- array_shift($temp);
- foreach($temp as $key3 => $value3){
- if($key3 == count($temp)-1){
- echo "$value3\n ";
- }else{
- echo "$value3 ";
- }
- }
- $history = array();
- foreach($temp as $key2 => $value2){
- if($key2 == 0){
- $length = strlen($value2);
- $history[] = $value2;
- }else{
- if($value2 != strtolower($value2)){
- //string is not all lower case
- echo "contains other than lower-case letters \"$value2\"\n";
- break;
- }
- if($length != strlen($value2)){
- //string is on inconsistent length
- echo "Inconsistent word length for \"$value2\"\n";
- break;
- }
- if(levenshtein($value2, $history[$key2-1]) != 1){
- //more than 1 changes
- echo "Too many changes to produce \"$value2\"\n";
- break;
- }
- if(in_array($value2, $history)){
- //string is already in the array
- echo "Repeated word \"$value2\"\n";
- break;
- }
- if(str_replace($letters, '', $value2) != ''){
- //string contains non-letters
- echo "Non-letters in \"$value2\"\n";
- break;
- }
- $history[] = $value2;
- }
- if($key2 == count($temp)-1){
- echo "Feasible\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment