Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $input = <<<END
- 1243
- 1234
- 1243
- 1243
- 1243
- 1234
- 1234
- 1233
- 1222
- 1211
- 1233
- 1234
- 1222
- 1211
- 1211
- 1210
- -1
- END;
- $input = explode("\n", $input);
- array_pop($input); //remove the negative 1
- sort($input); //put elements in order
- $max_length = null;
- foreach($input as $key => $value){
- $has_dupes = 0;
- foreach($input as $key2 => $value2){
- if($value == $value2 && $key != $key2){
- $has_dupes = 1;
- }
- }
- if(strlen($value) > $max_length){
- $max_length = strlen($value);
- }
- $full_array[$key]['has_dupe'] = $has_dupes;
- $full_array[$key]['val'] = $value;
- }
- foreach($input as $value){
- echo str_pad($value, $max_length+1, ' ', STR_PAD_RIGHT);
- }
- echo "\n";
- $rank = 0;
- $prev = null;
- foreach($full_array as $value){
- if($value['val'] != $prev){
- $rank++;
- }
- $prev = $value['val'];
- if($value['has_dupe']){
- echo str_pad($rank.'eq', $max_length+1, ' ', STR_PAD_RIGHT);
- }else{
- echo str_pad($rank, $max_length+1, ' ', STR_PAD_RIGHT);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment