ClarkeRubber

UNSW ProgComp: Problem 1 - 2006

Jun 9th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.98 KB | None | 0 0
  1. <?php
  2. $input = <<<END
  3. 1243
  4. 1234
  5. 1243
  6. 1243
  7. 1243
  8. 1234
  9. 1234
  10. 1233
  11. 1222
  12. 1211
  13. 1233
  14. 1234
  15. 1222
  16. 1211
  17. 1211
  18. 1210
  19. -1
  20. END;
  21.  
  22. $input = explode("\n", $input);
  23. array_pop($input); //remove the negative 1
  24. sort($input); //put elements in order
  25.  
  26. $max_length = null;
  27.  
  28. foreach($input as $key => $value){
  29.     $has_dupes = 0;
  30.     foreach($input as $key2 => $value2){
  31.         if($value == $value2 && $key != $key2){
  32.             $has_dupes = 1;
  33.         }
  34.     }
  35.     if(strlen($value) > $max_length){
  36.         $max_length = strlen($value);
  37.     }
  38.     $full_array[$key]['has_dupe'] = $has_dupes;
  39.     $full_array[$key]['val'] = $value;
  40. }
  41.  
  42. foreach($input as $value){
  43.     echo str_pad($value, $max_length+1, ' ', STR_PAD_RIGHT);
  44. }
  45.  
  46. echo "\n";
  47.  
  48. $rank = 0;
  49. $prev = null;
  50. foreach($full_array as $value){
  51.     if($value['val'] != $prev){
  52.         $rank++;
  53.     }
  54.     $prev = $value['val'];
  55.     if($value['has_dupe']){
  56.         echo str_pad($rank.'eq', $max_length+1, ' ', STR_PAD_RIGHT);
  57.     }else{
  58.         echo str_pad($rank, $max_length+1, ' ', STR_PAD_RIGHT);
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment