• tetris11
    link
    fedilink
    arrow-up
    2
    ·
    7 months ago

    Second attempt that factors in cheating.

    spoiler
    from numpy import median
    from random import choice
    from pprint import pprint
    
    # Functions
    get_mode_coin = lambda x: int(median(x))
    def pick(player, wants):
        for neighbor in players:
            if player != neighbor:
                neighbor_purse = players[neighbor]["purse"]
                if wants:
                    if wants in neighbor_purse: # Cheat
                        players[play]["purse"] = players[play]["purse"] + [wants]
                        continue
                players[play]["purse"] = players[play]["purse"] + [choice(neighbor_purse)]
    
    
    # Main
    players = {"p1" : {"purse": [1,0,1], "wants": False}, ## playing fair
               "p2" : {"purse": [0,0,1], "wants": 0}, ## cheating
               "p3" : {"purse": [1,1,0], "wants": 1}, ## cheating
               "p4" : {"purse": [1,1,0], "wants": 0}, ## cheating
               "p5" : {"purse": [0,0,1], "wants": False}}   ## playing fair
    
    for play in players: ## Players pick a desired coin from each of their neighbours
        pick(play, players[play]["wants"])
    print("First picks:")
    pprint(players)
    
    for play in players: ## Players collapse their collections to mode
        players[play] = [get_mode_coin(players[play]["purse"])]
    print("Last modes:", players)
    
    print("Final choice:", get_mode_coin([x for x in players.values()]))
    

    So, my method doesn’t work