def hot_potato_spicy(name_list, num): """ Simulates the Hot potato game "spiced up" with lives and randomness :param name_list: a list containing the name of the participants and their number of lives :param num: the counting constant (e.g., the length of each round of the game) :return: the winner """ def oneRound(): for k in range(num): dequed = Q.dequeue() Q.enqueue(dequed) if random()>0.5: return oneRound() else: roundLoser = Q.first() if lives[roundLoser] > 1: lives[roundLoser] -= 1 else: gameLoser = Q.dequeue() print(f'{gameLoser} has been eliminated from the game!') Q = ArrayQueue() lives = {player[0]: player[1] for player in name_list} for i in name_list: Q.enqueue(i[0]) while Q.__len__() != 1: oneRound() winner = Q.first() return winner
def hot_potato(name_list, num): """ Hot potato simulator. While simulating, the name of the players eliminated should also be printed (Note: you can print more information to see the game unfolding, for instance the list of players to whom the hot potato is passed at each step...) :param name_list: a list containing the name of the players, e.g. ["John", "James", "Alice"] :param num: the counting constant (i.e., the length of each round of the game) :return: the winner (that is, the last player standing after everybody else is eliminated) """ Q = ArrayQueue() for i in name_list: Q.enqueue(i) while Q.__len__() != 1: for k in range(num): dequed = Q.dequeue() Q.enqueue(dequed) removed = Q.dequeue() print(f'{removed} has been removed from the game!') winner = Q.first() return winner