Shagun Chhikara
Shagun Chhikara

Reputation: 155

Python Error with producing key of max of mins

I'm trying to write a function that consumes a dictionary where each key is assigned to a list of values. This function plays out a game where the minimum value from each key is taken and compared, the maximum value of the minimum values pertaining to the winner of the game. If all the players tie with their minimum values, their second smallest values are then compared, and the maximum from that produces the winning key. The solution must involve dictionaries/classes/loops but NO sets or recursion. Recursion may be used to break the loop.

For example:

determine_winner({'A':[1,1], 'B':[2,2], 'C':[1,1]}) produces 'B' (as B's minimum score of 2 is greater than the other players' minimum score of 1

determine_winner({'A':[1,2,3,4], 'B':[2,3,4,1], 'C':[1,3,4,5]}) produces 'C' (all players initially tie with a minimum score of 1, but C's next minimum is 3, while A and B's next minimum is 2)

determine_winner({'A':[1,2,3,4], 'B':[2,3,4,1], 'C':[1,4,1,5]}) produces 'Tied' (all players initially tie with a minimum score of 1, but then A and B tie with 2 while C has another 1 so it is no longer considered. Then A and B tie again with 3, and finally with 4, so the tie cannot be broken)

What I've written so far is producing an error:

def determine_winner(results):
    a = []
    max_mins = 0
    for key in results:
        if min(results[key]) > max_mins:
            winner = key
            max_mins = min(results[key])
        if min(results[key]) == max_mins:
            results = results[key].remove(min(results[key]))
    return winner

Upvotes: 1

Views: 82

Answers (1)

Brent Washburne
Brent Washburne

Reputation: 13158

Looks like you are modifying results while looping over it:

results = results[key].remove(min(results[key]))

Removing that last if statement will fix the error.

For the actual program, this version first sorts the results and then loops over them for each scorer:

def determine_winner(results):
    print results
    for key in results:
        results[key].sort()          # sort all the results
        length = len(results[key])

    for l in range(length):          # examine the scores in order
        max_score = 0
        next_results = {}
        for key in results:          # compare each scorer
            score = results[key][l]
            if score < max_score:    # ignore this scorer
                continue
            if score == max_score:   # multiple high scores
                winner = 'Tied'
            else:                    # new high score
                winner = key
                max_score = score
            # prepare the results for the next round
            next_results[key] = results[key]
        results = next_results       # get ready for the next round
    print winner

determine_winner({'A':[1,1], 'B':[2,2], 'C':[1,1]})
determine_winner({'A':[1,2,3,4], 'B':[2,3,4,1], 'C':[1,3,4,5]})
determine_winner({'A':[1,2,3,4], 'B':[2,3,4,1], 'C':[1,4,1,5]})

The output looks like this:

{'A': [1, 1], 'C': [1, 1], 'B': [2, 2]}
B
{'A': [1, 2, 3, 4], 'C': [1, 3, 4, 5], 'B': [2, 3, 4, 1]}
C
{'A': [1, 2, 3, 4], 'C': [1, 4, 1, 5], 'B': [2, 3, 4, 1]}
Tied

Upvotes: 2

Related Questions