Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
7 steps solver solution in Creative category for Bulls and Cows by Sim0000
# This code can solve all problems within 7 steps
from itertools import permutations
def calc(x, y):
bulls = sum(x[i] == y[i] for i in range(4))
return (bulls, sum(t in y for t in x) - bulls)
# Knuth's height method (special thanks to Ciel)
def minmax(e):
freq = {}
for f in possible:
x = calc(e, f)
if x not in freq: freq[x] = 0
freq[x] += 1
return (max(freq.values()), e)
def checkio(data):
global possible
# 1st question
if not data: # 0
possible = permutations('0123456789', 4)
return '0123'
# make possible list
last = (int(data[-1][5]), int(data[-1][7])) # (bulls, cows)
possible = [x for x in possible if calc(data[-1][:4], x) == last]
# decide question (average 6.54 max 7, fast)
if len(data) < 4: # 1,2,3
question = possible[len(possible) // 2]
elif len(data) < 6: # 4,5
question = min(minmax(e) for e in permutations('0123456789', 4))[1]
else: # 6
question = min(minmax(e) for e in possible)[1]
return ''.join(map(str, question))
"""
# decide question (average 5.97, max 7, slow)
if len(data) < 3: # 1,2
question = possible[len(possible) // 2]
elif len(data) < 5: # 3,4
question = min(minmax(e) for e in permutations('0123456789', 4))[1]
else: # 5,6
question = min(minmax(e) for e in possible)[1]
return ''.join(map(str, question))
"""
# feature
# - progressive possible list construct
# - Knuth's height method (Special thanks to Ciel)
# - question strategy
March 6, 2014
Comments: