Advertisement
Guest User

Football Standings

a guest
Feb 20th, 2020
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.88 KB | None | 0 0
  1. <?php
  2. $key = readline();
  3. $teams = [];
  4. while (true) {
  5.     $command = readline();
  6.     if ($command == "final") {
  7.         break;
  8.     }
  9.     $input = explode(" ", $command);
  10.     $homeTeamDecrypt = $input[0];
  11.     $awayTeamDecrypt = $input[1];
  12.     $homeTeam = strtoupper(strrev(explode($key, $homeTeamDecrypt)[1]));
  13.     $awayTeam = strtoupper(strrev(explode($key, $awayTeamDecrypt)[1]));
  14.     $goals = explode(":", $input[2]);
  15.     $goalsHomeTeam = $goals[0];
  16.     $goalsAwayTeam = $goals[1];
  17.  
  18.     $pointsHomeTeam = 1;
  19.     $pointsAwayTeam = 1;
  20.     if ($goalsHomeTeam > $goalsAwayTeam) {
  21.         $pointsHomeTeam = 3;
  22.         $pointsAwayTeam = 0;
  23.     } elseif ($goalsHomeTeam < $goalsAwayTeam) {
  24.         $pointsHomeTeam = 0;
  25.         $pointsAwayTeam = 3;
  26.     }
  27.     addInfoToTeams($teams, $homeTeam, $pointsHomeTeam, $goalsHomeTeam);
  28.     addInfoToTeams($teams, $awayTeam, $pointsAwayTeam, $goalsAwayTeam);
  29. }
  30.  
  31. echo "League standings:" . PHP_EOL;
  32. uksort($teams, function ($a, $b) use ($teams) {
  33.     if ($teams[$a]["points"] === $teams[$b]["points"]) return strcmp($a, $b);
  34.     return $teams[$b]["points"] - $teams[$a]["points"];
  35. });
  36. $position = 1;
  37. foreach ($teams as $key => $value) {
  38.     echo "$position. $key {$value['points']}" . PHP_EOL;
  39.     $position++;
  40. }
  41. echo "Top 3 scored goals:" . PHP_EOL;
  42. uksort($teams, function ($a, $b) use ($teams) {
  43.     if ($teams[$a]["goals"] === $teams[$b]["goals"]) return strcmp($a, $b);
  44.     return $teams[$b]["goals"] - $teams[$a]["goals"];
  45. });
  46. $teams = array_slice($teams, 0, 3);
  47. foreach ($teams as $key => $value) {
  48.     echo "- $key -> {$value['goals']}" . PHP_EOL;
  49. }
  50.  
  51. function addInfoToTeams(&$teams, $team, $points, $goals) {
  52.     if (!key_exists($team, $teams)) {
  53.         $teams[$team] = [
  54.             "points" =>  0,
  55.             "goals" => 0
  56.         ];
  57.     }
  58.     $teams[$team]['points'] += $points;
  59.     $teams[$team]['goals'] += $goals;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement