Last active
June 2, 2016 23:40
-
-
Save ascv/9806a7ba8924763fa5bc9821de462c2e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 getpass | |
def compute_freqs(sequence): | |
freqs = { | |
'd|d' : 0, | |
'f|d' : 0, | |
'd|f' : 0, | |
'f|f' : 0, | |
} | |
prev = sequence[0] | |
for char in sequence: | |
cur = char | |
freqs["%s|%s" % (cur, prev)] += 1 | |
prev = cur | |
return freqs | |
def compute_probs(freq): | |
d_count = float(freq['d|d'] + freq['f|d']) | |
f_count = float(freq['d|f'] + freq['f|f']) | |
probs = { | |
'd|d' : freq['d|d']/d_count if d_count > 0 else 0, | |
'f|d' : freq['f|d']/d_count if d_count > 0 else 0, | |
'd|f' : freq['d|f']/f_count if f_count > 0 else 0, | |
'f|f' : freq['f|f']/f_count if f_count > 0 else 0, | |
} | |
return probs | |
print "FREE WILL GAME v 1.0" | |
print "Randomly type 'd' or 'f' for a little while. You must press <enter> after typing each character." | |
brain_size = 20 | |
sequence = [] | |
i = 0 | |
while True: | |
letter = getpass.getpass('') | |
if letter not in ('d' , 'f'): | |
print "Please input only 'd' or 'f'." | |
continue | |
i += 1 | |
sequence.append(letter) | |
if i == brain_size: | |
break | |
accuracy = [] | |
probs = compute_probs(compute_freqs(sequence)) | |
while True: | |
prev = sequence[-1] | |
prob_d = probs["d|%s" % prev] | |
prob_f = probs["f|%s" % prev] | |
guess = 'd' if prob_d > prob_f else 'f' | |
cur = getpass.getpass('>') | |
if cur not in ('d' , 'f'): | |
print "Please input only 'd' or 'f'." | |
continue | |
if guess == cur: | |
accuracy.append(1) | |
else: | |
accuracy.append(0) | |
if len(accuracy) > brain_size: | |
accuracy.pop(0) | |
correct = 1.0 * sum(accuracy) | |
total = len(accuracy) | |
result = correct/total | |
print "%.4f" % result | |
sequence.append(cur) | |
sequence.pop(0) | |
probs = compute_probs(compute_freqs(sequence)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment