def get_winner(players): """Given a list of Player namedtuples return the player with the highest score using calculate_score. If the length of the scores lists of the players passed in don't match up raise a ValueError. Returns a Player namedtuple of the winner. You can assume there is only one winner. For example - input: Player(name='player 1', scores=[1, 3, 2, 5]) Player(name='player 2', scores=[1, 1, 1, 1]) Player(name='player 3', scores=[4, 5, 1, 2]) output: Player(name='player 3', scores=[4, 5, 1, 2]) """ scores = [] len_score = [] for player in players: scores.append((player, calculate_score(player.scores))) len_score.append(len(player.scores)) # ensure that the length of scores matches if min(len_score) != max(len_score): raise ValueError("something fishy about the length of scores!") result = sorted(scores, key=lambda x: x[1], reverse=True)[0][0] return result
def test_calculate_score(): assert calculate_score([1, 3, 2, 5]) == 5 assert calculate_score([1, 4, 2, 5]) == 9 assert calculate_score([1, 1, 1, 1]) == 0 assert calculate_score([4, 5, 1, 2]) == 9 assert calculate_score([6, 6, 5, 5]) == 22
def test_calculate_score_str_raises_exception(): with pytest.raises(ValueError): calculate_score([4, -5, 6, 2])
def test_calculate_score_non_int_raises_exception(): with pytest.raises(ValueError): calculate_score([4, 5, 6, 'a'])
def test_calculate_score_negative_numbers_raises_exception(): with pytest.raises(ValueError): calculate_score([4, -5, -1, 2])
def test_wrong_inputs(arg): with pytest.raises(ValueError): calculate_score(arg)
def test_calculate_score(arg, expected): assert calculate_score(arg) == expected