#!/usr/bin/python
import sys
# Convert an integer with length<=4 into a 4-element list
def int2list(i):
L=[]
L.append(i/1000)
L.append(i/100%10)
L.append(i/10%10)
L.append(i%10)
return L
# Compare number x and y, and return (A,B).
def nanb(x, y):
(a, b)=(0, 0)
(L1, L2) = (int2list(x), int2list(y))
for i in L1:
if i in L2:
if L1.index(i) == L2.index(i):
a=a+1
else:
b=b+1
return (a, b)
# Check whether 'guess' and 'i' satisfy the condition nAnB.
def possible(guess, i, a, b):
if (a, b) == nanb(guess, i):
return 1
else:
return 0
def readinput():
print "Guess: ",
guess = int(sys.stdin.readline())
print "A= ",
a = int(sys.stdin.readline())
print "B= ",
b = int(sys.stdin.readline())
return (guess, a, b)
# Main program starts here:
(guess, a, b) = readinput()
suspect=[]
candidates = 0
for i in range(10000):
if i/1000 == i/100%10 or i/1000 == i/10%10 or i/1000 == i%10 or i/100%10 == i/10%10 or i/100%10 == i%10 or i/10%10 == i%10 or possible(guess, i, a, b) == 0:
suspect.append(0)
else:
print i,
candidates = candidates + 1
suspect.append(1)
print "\n %d candidate(s) totally." % candidates
while 1:
(guess, a, b) = readinput()
candidates =0
for i in range(10000):
if suspect[i] == 1:
suspect[i] = possible(guess, i, a, b)
if suspect[i]==1:
print i,
candidates = candidates + 1
print "\n %d candidate(s) totally" % candidates
if candidates ==1:
break
--
※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw)
◆ From: 140.112.252.172
※ 編輯: wyatt 來自: 140.112.252.172 (05/06 18:26)
※ 編輯: wyatt 來自: 140.112.252.172 (05/06 19:04)
※ 編輯: wyatt 來自: 140.112.252.172 (05/06 19:05)