The Friday Fragment

Published on 5 February 2011
This post thumbnail

It's Friday, and time again for the Friday Fragment: our weekly programming-related puzzle.

This Week's Fragment

Let's continue our team ranking system based on win/loss records and strength of schedule (SOS):

Write code to calculate the RPI (Ratings Percentage Index) for a set of teams using the basic Wikipedia calculation. Present the teams ordered by RPI.

To play along, post your code or URL as a comment, or send it via email. If you'd like, you can build atop last week's code and data.

Last Week's Fragment - Solution

Last week's puzzle introduced us to strength of schedule calculations for NCAA teams, or any team for that matter:

Write code to find the strength of schedule (SOS) for your favorite team using the basic Wikipedia calculation. Start with a simple list of wins and losses, parse the results, and calculate the ratio. For extra insight, compare it to other teams played.

I wrote a basic PHP Team class to parse the results, house the records and opponents, and calculate strength of schedule. Here's the gist of it:

   public function computeStrengthOfSchedule() {
      foreach ($this->getOpponents() as $opponent) {
          if (!is_null($team = self::getTeam($opponent))) {
              $opponentWins += $team->getWins();
              $opponentGames += $team->getWins() + $team->getLosses();
              foreach ($team->getOpponents() as $opponentOpponent) {
                  if (!is_null($team2 = self::getTeam($opponentOpponent))) {
                       $opponentOpponentWins += $team2->getWins();
                       $opponentOpponentGames += $team2->getWins() + $team2->getLosses();
      if ($opponentGames > 0)
          $or = $opponentWins / $opponentGames;
      if ($opponentOpponentGames > 0)
          $oor = $opponentOpponentWins / $opponentOpponentGames;
      $this->setStrengthOfSchedule(((2*$or) + $oor)/3);

You can run this at  /files/fragments/sos.html, which also includes links to the source code and 2010 NCAA Football data.