Thursday, December 19, 2019

Exercise: Super Epic Competition Score Board

Effective Python Chapter 1 Exercise

We have been tasked with creating a score board for the Super Epic Competition. All the work for scoring the competition is already done, and we will be given a list of users that are ordered by the place they took in the competition. We need to take that list and format and print out a score board according to the following spec:

The score board will print out all participants ordered by their place in the competition. There are a max of 8 participants per competition. Each line will have a single participant. On the left side we start with their rank, followed by their user name. If the user is part of a team (which can be determined by whether or not the user object has a team name field), then the team name should follow in parentheses. Both user name and team name have a max limit of 20 characters. On the right their score will be displayed. In this competition the score is a decimal number that can have any number of decimal places. On the score board this number will be formatted to two decimal places. Also note that is is possible for the list of users to be returned as empty, in which case the message "Awaiting Final Ranking" should be displayed. Here are a few examples of input and expected output:

Example 1

Input
[]
Expected Output
Awaiting Final Ranking

Example 2

Input
[
  {
    "user_name": "hamster dance",
    "score": 1337
  },
  {
    "user_name": "success kid",
    "team_name": "nerf herders",
    "score": 1200.333333
  },
  {
    "user_name": "rickroll",
    "team_name": "the fellowship",
    "score": 1051.4
  },
  {
    "user_name": "ArrowToTheKnee",
    "team_name": "nerf herders",
    "score": 999.99
  },
  {
    "user_name": "grumpy_cat",
    "team_name": "the fellowship",
    "score": 999.98
  },
  {
    "user_name": "anonymous",
    "score": 561.12
  },
  {
    "user_name": "lol cat",
    "team_name": "allz te lolz",
    "score": 98.0432
  },
  {
    "user_name": "awkward seal",
    "score": -20
  }
]
Expected Output
1 hamster dance                                   1337.00
2 success kid (nerf herders)                      1200.33
3 rickroll (the fellowship)                       1051.40
4 ArrowToTheKnee (nerf herders)                    999.99
5 grumpy_cat (the fellowship)                      999.98
6 anonymous                                        561.12
7 lol cat (allz te lolz)                            98.04
8 awkward seal                                     -20.00
The answer to this exercise can be found here.