ClarkeRubber

UNSW ProgComp: Problem 5 - 2008

Jun 13th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.61 KB | None | 0 0
  1. <?php
  2.  
  3. $input = <<<END
  4. 416 12 4
  5. So Isaac dwelt in Gerar and the men of the place asked him
  6. of his wife and he said, "She is my sister," for he feared to say
  7. "She is my wife", lest, said he, "the men of the place
  8. should kill me for Rebekah because she was fair to look upon"
  9. and it came to pass when he had been there a long time that Abimelech,
  10. king of the Philistines, looked out at a window and saw, and, behold,
  11. Isaac was sporting with Rebekah his wife and Abimelech called Isaac
  12. and said, "Behold of a surety she is thy wife and how saidst thou
  13. 'she is my sister'?" and Isaac said unto him, "Because, I said, lest I
  14. die for her."
  15. Genesis 26:6-9. King James version.
  16. END;
  17.  
  18. $input = explode("\n", $input);
  19.  
  20. $input_2 = explode(" ", array_shift($input)); //offset, cycle (distance), length
  21.  
  22. $input = implode("\n", $input); //return to long string
  23. $input = str_split($input);
  24.  
  25. $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');
  26. $axed_string = str_replace($letters, '', strtolower(implode($input)));
  27. $axed_characters = str_split($axed_string);
  28. $new_input = str_replace($axed_characters, '', $input);
  29.  
  30. //I'm going to do this the stupid way
  31. if($input_2[1] < 0){
  32.     //going to have to work the other way
  33.     $count = count($input);
  34.     $disp_count = 0;
  35.     $char_count = count($new_input);
  36.     $str_out = null;
  37.     while($count > 0){
  38.         $current = $input[$count];
  39.         $echoed = false;
  40.         if(in_array(strtolower($current), $letters)){
  41.             //current is a letter
  42.             if(($char_count-$input_2[0]-1)%abs($input_2[1]) == 0 && $disp_count < $input_2[2] && $char_count < $input_2[0]+2){
  43.                 //display character with [] and add the character to the output string
  44.                 $disp_count++;
  45.                 $output[] = $current;
  46.                 $str_out[] = "[".$current."]";
  47.                 $echoed = true;
  48.             }
  49.             $char_count--;
  50.         }
  51.         if(!$echoed){
  52.             $str_out[] = $current;
  53.         }
  54.         $count--;
  55.     }
  56.     $str_out = implode(array_reverse($str_out));
  57.     $output = implode(array_reverse($output));
  58.     echo $str_out;
  59. }else{
  60.     $count = 0;
  61.     $disp_count = 0;
  62.     $char_count = 0;
  63.     while($count < count($input)){
  64.         $current = $input[$count];
  65.         $echoed = false;
  66.         if(in_array(strtolower($current), $letters)){
  67.             //current is a letter
  68.             if(($char_count-$input_2[0]+1)%abs($input_2[1]) == 0 && $disp_count < $input_2[2] && $char_count > $input_2[0]-2){
  69.                 //display character with [] and add the character to the output string
  70.                 $disp_count++;
  71.                 $output .= $current;
  72.                 echo "[".$current."]";
  73.                 $echoed = true;
  74.             }
  75.             $char_count++;
  76.         }
  77.         if(!$echoed){
  78.             echo $current;
  79.         }
  80.         $count++;
  81.     }
  82. }
  83.  
  84. echo "\n\n$output";
Advertisement
Add Comment
Please, Sign In to add comment