Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
def test_calculate_score_str_raises_exception():
    with pytest.raises(ValueError):
        calculate_score([4, -5, 6, 2])
Ejemplo n.º 4
0
def test_calculate_score_non_int_raises_exception():
    with pytest.raises(ValueError):
        calculate_score([4, 5, 6, 'a'])
Ejemplo n.º 5
0
def test_calculate_score_negative_numbers_raises_exception():
    with pytest.raises(ValueError):
        calculate_score([4, -5, -1, 2])
Ejemplo n.º 6
0
def test_wrong_inputs(arg):
    with pytest.raises(ValueError):
        calculate_score(arg)
Ejemplo n.º 7
0
def test_calculate_score(arg, expected):
    assert calculate_score(arg) == expected