The hat problem goes like this: Three players enter a room and a red or blue hat is placed on each person's head. The color of each hat is determined by a coin toss, with the outcome of one coin toss having no effect on the others. Each person can see the other players' hats but not his own.

No communication of any sort is allowed, except for an initial strategy session before the game begins. Once they have had a chance to look at the other hats, the players must simultaneously guess the color of their own hats or pass. The group shares a hypothetical $3 million prize if at least one player guesses correctly and no players guess incorrectly.'''

defstrategy0(otherhats, hatcolours): ''' If there is a clear max total of hat colours: Yours is a random choice from the hat colours occuring the least else: pass'''# Count the total hats in each colour colourcount = [otherhats.count(c) for c in hatcolours]# What hat colours are most/least prevalent mn = min(colourcount) mx = max(colourcount)#if colourcount.count(mx) == 1:# only one colour occurs as the maximumreturn hatcolours[random.choice([ indexfor index, count in enumerate(colourcount)if count == mn]) ]else:return ipass

defstrategy1(otherhats, hatcolours): ''' If there is a clear max and min total of hat colours, Yours is the min; otherwise pass.'''# Count the total hats in each colour colourcount = [otherhats.count(c) for c in hatcolours]# What hat colours are most/least prevalent mn = min(colourcount) mx = max(colourcount)#if mn != mx and (mn == 0 or colourcount.count(mx) == colourcount.count(mn) == 1):# only one colour occurs as the maximum, another as minimum# So guess the minimumreturn hatcolours[colourcount.index(mn)]else:return ipass