Doubt about "resorteio" of equal values

Asked

Viewed 41 times

4

I did a giveaway exercise using dictionary:

from random import randint               
from time import sleep              
from operator import itemgetter

jogo = {'jogador 1': randint(1,6), 'jogador 2': randint(1,6), 'jogador 3': randint(1,6), 'jogador 4': randint(1,6)}                                                                              
ranking = {}                      
print('Valores Sorteados')              
for k,v in jogo.items():                     
    print(f' {k} tirou {v} no dado')                                                    
    sleep(1)

ranking = sorted(jogo.items(), key=itemgetter(1), reverse=True)             
for k, v in enumerate(ranking):             
    print(f'{k+1}º lugar: {v[0]} com {v[1]}')

However, sometimes players have equal values, as is the case in the example below. Players 3 and 2 have the same value (4), but only player 3 is in 2nd place.

1º lugar: jogador 4 com 5              
2º lugar: jogador 3 com 4 <-       
3º lugar: jogador 2 com 4 <-  
4º lugar: jogador 1 com 2

How do I conduct a new draw only between players with equal values to play 2nd and 3rd?

Ex:

1º lugar: jogador 4 com 5                                     
2º lugar: jogador 3 com 4  
3º lugar: jogador 2 com 4  
4º lugar: jogador 1 com 2   

2º lugar: jogador 3 com 4  -> RESORTEIO -> jogador 3 com 2                          
3º lugar: jogador 2 com 4  -> RESORTEIO -> jogador 2 com 6                                                 

1º lugar: jogador 4 com 5                                     
2º lugar: jogador 2 com (4 na 1º tentativa, 6 no resorteio)               
3º lugar: jogador 3 com (4 na 1º tentativa, 2 no resorteio)                    
4º lugar: jogador 1 com 2  
  • 1

    this will give work, it would not be easier to generate the players one by one, and when assigning the random value see if it already exists and assign another next?

1 answer

4

It would even be possible to ask the code to repeat the draws of all players, or only of the players with the same number, but consider the following approach instead:

  • start a numerical sequence with the possible values for the game:

    # valores possíveis = [1, 2, 3, 4, 5, 6]
    valores = list(range(1, 7))
    
  • we scramble this sequence:

    import random
    random.shuffle(valores)
    
  • we set the total number of players

    max_jogadores = 4
    
  • create a dictionary based on this parameter, sequentially taking the elements of the sequence of values. That’s the key point: as the sequence is composed of unique elements and is scrambled, we can simply assign each element of the sequence to each player in order, without worrying about repetitions.

    jogo = {f'jogador {i+1}': valores[i] for i in range(max_jogadores)}
    

Output (in my case, varies with each execution):

{'jogador 1': 3, 'jogador 2': 5, 'jogador 3': 6, 'jogador 4': 2}

An interesting detail is that the line jogo = ... will cause a IndexError if the number of players is greater than the values available in your list. It may sound bad, but if this happened to your original idea, the code would silently go in an infinite loop when trying to assign unique values to a number of players higher than the number of possible values.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.