Answer to Effective Python Chapter 1 Exercise
This is the answer to the exercise defined here.To begin with, let's throw together some basic scaffolding to help us test and build out our solution. We can take the input examples from the exercise and put them into python as follows:
example_1 = [] example_2 = [ { "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 } ]And we can set up a basic way to test it by doing this:
print(format_score_board(example_1), format_score_board(example_2), sep="\n\n")After that, we just need to define our format_score_board function. We can solve the exercise by defining the following code:
def format_score_board(users: List[dict]) -> str: if not users: return "Awaiting Final Ranking" result = "" for rank, user in enumerate(users, 1): if team_name := user.get('team_name', ''): team_name = f" ({team_name})" line = f"{rank} {(user['user_name'] + team_name):<43} {user['score']:8.2f}" result += "\n" + line return resultNow for a little bonus extra credit. What if we wanted to make this just a little bit nicer? What if we wanted to add some leader dots to the blank space between the left and right sides, and perhaps add commas to the scores to make them more readable? How could we change this to do that? With a couple of simple changes, we can get this code:
def format_score_board(users: List[dict]) -> str: if not users: return "Awaiting Final Ranking" result = "" for rank, user in enumerate(users, 1): user_name = user['user_name'] if team_name := user.get('team_name', ''): team_name = f" ({team_name})" score = user['score'] line = f"{rank} {(user_name + team_name):{'.'}<43}.{score:{'.'}>9,.2f}" result += "\n" + line return result