NBA

The NBA Basketball League rankings are stored in a file where in each line there is the data concerning a team: team name (str), name of the division where the team plays (str) and the result, separated by the string ' : ' (a space, a colon and a space). The result is represented with the number of games won and lost (two int) separated by a hyphen ('-'). For an example, of this kind of file, see file nba.txt with the following content:

Boston Celtics : Atlantic : 11-9
Indiana Pacers : Central : 9-11
Utah Jazz : Northwest : 13-10
Portland Trail Blazers : Northwest : 12-11
Brooklyn Nets : Atlantic : 9-13

Save the following functions into the file nba.py.

  1. Write the function good_teams() that given the name of one file as the one indicated (str) and number (int), n, returns a list with the names (str) of those teams that have won more than n games, sorted alphabetically. Examples:

    >>> good_teams('nba.txt', 12)
    ['New Orleans Pelicans', 'Oklahoma City Thunder', 'Orlando Magic', 'Phoenix Suns', 'San Antonio Spurs', 'Toronto Raptors', 'Utah Jazz']
    >>> good_teams('nba.txt', 15)
    ['San Antonio Spurs']
    >>> good_teams('nba.txt', 11)
    ['Chicago Bulls', 'New Orleans Pelicans', 'Oklahoma City Thunder', 'Orlando Magic', 'Phoenix Suns', 'Portland Trail Blazers', 'San Antonio Spurs', 'Toronto Raptors', 'Utah Jazz']
    >>> good_teams('nba.txt', 18)
    []
    

    Note

    More tests are provided in file test-good_teams.txt

  2. Write the function group() that given the name of a file as indicated (str), returns a dictionary (dict) where the key is the division (str) and the value the (list) of those teams (str) belonging to that division, sorted alphabetically.

    >>> d = group('nba.txt')
    >>> d == {'Atlantic': ['Boston Celtics', 'Brooklyn Nets', 'Toronto Raptors'],
    ... 'Central': ['Chicago Bulls', 'Cleveland Cavaliers', 'Indiana Pacers'],
    ... 'Northwest': ['Oklahoma City Thunder', 'Portland Trail Blazers',
    ... 'Utah Jazz'], 'Southeast': ['Orlando Magic', 'Washington Wizards'],
    ... 'Pacific': ['Los Angeles Lakers', 'Phoenix Suns', 'Sacramento Kings'],
    ... 'Southwest': ['Dallas Mavericks', 'New Orleans Pelicans', 'San Antonio Spurs']}
    True
    

    Note

    More tests are provided in file test-group.txt

  3. Write the function win() that given the name of a file as indicated (str) and a division (str), returns a (list) with the teams (str) of that division such that the number of matches they have won is greater than the number of matches they have lost. The elements in this list must be tuples (tuple) of two elements: team name (str) and number of won matches (int) and have to be sorted in decreasing order by the number of won matches.

    >>> win('nba.txt', 'Atlantic')
    [('Toronto Raptors', 15), ('Boston Celtics', 11)]
    >>> win('nba.txt', 'Central')
    [('Chicago Bulls', 12), ('Cleveland Cavaliers', 11)]
    >>> win('nba.txt', 'Northwest')
    [('Utah Jazz', 13), ('Oklahoma City Thunder', 13), ('Portland Trail Blazers', 12)]
    >>> win('nba.txt', 'Pacific')
    [('Phoenix Suns', 14), ('Sacramento Kings', 11)]
    >>> win('nba.txt', 'Southwest')
    [('San Antonio Spurs', 16), ('New Orleans Pelicans', 13)]
    

    Note

    More tests are provided in file test-win.txt

Solutions

A solution of these functions is provided in file nba.py