In fact, I finished this assignment about 2 hours although this examination must require within one and a half hours.
import sys
vs = {}
for i in range(1,len(sys.argv)):
a = sys.argv[i].split("=")
vs[a[0]] = a[1]
hist = {}
num=5 # num of cards
ol=[]
for i in range(1,num+1):
ol.append(i)
a = int(vs["A"])
b = int(vs["B"])
c = int(vs["C"])
def simpleGuess(l,o1,o2):
if o1 in l:
l.remove(o1)
if o2 in l:
l.remove(o2)
if len(l)==0:
return '?'
if (max(l) < o1 and max(l) < o2):
return 'MIN'
elif (min(l) > o1 and min(l) > o2):
return 'MAX'
else:
b = True
for e in l:
if (e > o1 and e < o2) or (e > o2 and e < o1) \
or (e > o1 and e < o2) or (e > o2 and e < o1):
continue
else:
b = False
break
if (b == True):
return 'MID'
return '?'
def guess(nl,nl2):
for e in nl:
tl = list(nl)
tl.remove(e)
b = True
for i in range(len(tl)-1):
if (abs(tl[i]-tl[i+1])==1):
continue
else:
b = False
break
if (b and (e in nl2)):
nl2.remove(e)
# a
out = ""
ret = simpleGuess(list(ol),b,c)
out = 'A =>'+ret
if (ret!='?'):
print(out)
exit()
else:
out = out + ", "
while(1):
# b
nl2 = list(ol)
nl = list(ol)
nl.remove(c)
guess(nl,nl2)
ret = simpleGuess(nl2,a,c)
out = out + 'B =>'+ret
if (ret!='?'):
print(out)
exit()
else:
out = out + ", "
# c
nl = list(ol)
nl2 = list(ol)
nl.remove(a)
guess(nl,nl2)
ret = simpleGuess(nl2,a,b)
out = out + 'C =>'+ret
if (ret!='?'):
print(out)
exit()
else:
out = out + ", "
# a
nl = list(ol)
nl2 = list(ol)
nl.remove(b)
guess(nl,nl2)
ret = simpleGuess(nl2,b,c)
out = out + 'A =>'+ret
if (ret!='?'):
print(out)
exit()
else:
out = out + ", "
$ python a.py A=1 B=4 C=5
A =>MIN
$ python a.py A=1 B=2 C=4
A =>?, B =>MID
$ python a.py A=4 B=3 C=2
A =>?, B =>?, C =>MIN
$ python a.py A=1 B=3 C=2
A =>?, B =>MAX
$ python a.py A=1 B=3 C=5
A =>?, B =>MID
BTW, RCO engineers seem to hate PHPers...