Skip to content

Instantly share code, notes, and snippets.

@Sigafoos Sigafoos/highlow.py
Created Feb 19, 2017

Embed
What would you like to do?
Determine the success rates of algorithms for high/low
import random
from copy import deepcopy
# create the set of cards (2-10, JQKA) x4. suits don't matter.
deck = [x for x in xrange(2, 15)] * 4
# translate the numerical value to the string
def to_card(i):
if i == 11:
return 'J'
if i == 12:
return 'Q'
if i == 13:
return 'K'
if i == 14:
return 'A'
return str(i)
##############
# algorithms #
##############
def always_high(options):
return True
def always_low(options):
return False
def alternate(options):
return (sum(options['results']) % 2 == True)
def reverse_alternate(options):
return (sum(options['results']) % 2 == False)
def use_last(options):
return options['last_answer']
def reverse_use_last(options):
return not options['last_answer']
def probable(options):
highlow = [0, 0] # high, low
for card in deck:
if card - options['last_card'] > 0:
highlow[0] += 1
else:
highlow[1] += 1
return highlow[0] > highlow[1]
# do the calculations
def highlow(deck, algorithm, verbose=False):
results = [0, 0] # correct, incorrect
card = None
last_card = None
last_answer = False # for the first go
# as long as there are cards in the deck
while len(deck) > 0:
if last_card is not None and card is not None: # not the first time
last_answer = (card - last_card > 0)
last_card = card if card is not None else deck.pop()
# using the algorithm we provided, guess the next card
guess = globals()[algorithm]({'last_card': last_card, 'results': results, 'last_answer': last_answer, 'deck': deck})
card = deck.pop()
if (verbose):
print 'Last: %s. Current: %s. Guess: %s' % (to_card(last_card), to_card(card), 'Higher' if guess == True else 'Lower')
# were we right?
# this is really "higher or not higher", as equals counts as low
if (card - last_card > 0) == guess:
if (verbose):
print 'You were correct!'
results[0] += 1
else:
if (verbose):
print 'You were wrong.'
results[1] += 1
return results[0]
algorithms = ['always_low', 'always_high', 'alternate', 'reverse_alternate', 'use_last', 'reverse_use_last', 'probable']
results = {}
# create the dictonary of empty result lists
for algorithm in algorithms:
results[algorithm] = []
# go through a randomly shuffled deck 1000 times
for i in xrange(1000):
# the same deck will be used for each algorithm
random.shuffle(deck)
for algorithm in algorithms:
results[algorithm].append(highlow(deepcopy(deck),algorithm))
for algorithm in algorithms:
avg_correct = sum(results[algorithm]) / float(len(results[algorithm]))
print '%s:\t\t\t%s%%' % (algorithm, ((avg_correct / 51) * 100))
# create a comma-separated list
csv = ','.join(algorithms)
for i in xrange(len(results[algorithms[0]])):
csv += '\n' + ','.join(map(lambda x: str(results[x][i]), algorithms))
# write it to a file
with open('highlow.csv', 'w') as fp:
fp.write(csv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.