Created
February 19, 2017 20:22
-
-
Save Sigafoos/e23b2bb47cee407badd960d75854c50b to your computer and use it in GitHub Desktop.
Determine the success rates of algorithms for high/low
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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