# usr bin env python import random coms for in xrange 10 for in xrange 1

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 #!/usr/bin/env python import random coms = [] for i in xrange(10): for j in xrange(10): for k in xrange(10): for l in xrange(10): coms.append([i,j,k,l]) coms = filter (lambda x: x.count(x[0])==1 and x.count(x[1])==1 and x.count(x[2])==1,coms) his = []; hist = [] def ccount (inp): bulls = 0; cows = 0 for i in xrange (4): if inp[i] in secn: if inp[i] == secn[i]: bulls += 1 else: cows += 1 return [inp, bulls, cows] def generate (): if coms != []: tryn = random.choice (coms) elif len(coms) == 1: tryn = coms return tryn def clear(): global coms for i in xrange(len(his)): # bulls == 0 cows != 0 if hist[i][0] == 0 and hist[i][1] != 0: for j in xrange(4): coms = filter (lambda x: x[j] != his[i][j], coms) # bulls + cows == 4 if hist[i][0] + hist[i][1] == 4: for j in xrange(4): coms = filter (lambda x: x[j] in his[i], coms) # bulls + cows == 0 if hist[i][0] + hist[i][1] == 0: for j in xrange(4): coms = filter (lambda x: x[j] not in his[i], coms) # bulls + cows != 0 if hist[i][0] + hist[i][1] != 0: p = [] for j in xrange(4): if filter (lambda x: his[i][j] in x, coms) != []: p.append (his[i][j]) else: p.append (-1) p = filter(lambda x: x != -1,p) if len(p) == hist[i][0] + hist[i][1] and len(p) != 0: for j in xrange(len(p)): coms = filter (lambda x: p[j] in x ,coms) # bulls != 0 cows == 0 if hist[i][0] != 0 and hist[i][1] == 0: p = [] for j in xrange (4): if filter (lambda x: x[j]==his[i][j], coms) != []: p.append (his[i][j]) else: p.append (-1) if len(filter (lambda x: x != -1,p)) == hist[i][0]: for j in xrange (4): coms = filter ( lambda x: x[j] == p[j] or p[j] == -1, coms) ### for j in xrange(i+1,len(his)): # [1,2,3,4] and [5,6,7,8] -> [0,9] not in secn if filter (lambda x: x not in his[j], his[i]) == his[i]: if hist[i][0]+hist[i][1]+hist[j][0]+hist[j][1]==4: v = filter (lambda x: x not in his[i] and x not in his[j] , range(10)) coms = filter (lambda x: v[0] not in x or v[1] not in x ,coms) # [1,2,3,4] [1,2,3,5] if len(filter(lambda x: x[0]==x[1], zip(his[i],his[j]))) == 3: v = filter(lambda x: x[0]!=x[1], zip(his[i],his[j])) if hist[i][0] > hist[j][0]: coms = filter(lambda x: v[0][0] in x, coms) coms = filter(lambda x: v[0][0] == x[x.index(v[0][0])],coms) if hist[i][0] < hist[j][0]: coms = filter(lambda x: v[0][1] in x, coms) coms = filter(lambda x: v[0][1] == x[x.index(v[0][1])],coms) if hist[i][1] > hist[j][1]: coms = filter(lambda x: v[0][0] in x, coms) if hist[i][1] < hist[j][1]: coms = filter(lambda x: v[0][1] in x, coms) if hist[i][0] == hist[j][0] and hist[i][1] == hist[j][1]: #print v[0], his[i], his[j], secn #print "**",len(filter (lambda x: v[0][0] in x or v[0][1] in x,coms)),"**" coms != filter (lambda x: v[0][0] not in x or v[0][1] not in x,coms) #print "*", coms = filter (lambda x: x not in his, coms) secn = []; a = range(10) for i in xrange(4): secn.append (random.choice(a)) a.remove(secn[i]) it = 0; _win = 0 while _win == 0: out = ccount ( generate () ) his.append (out[0]); hist.append([]) hist[it].append (out[1]) hist[it].append (out[2]) tryn = out[0]; bulls = out[1]; cows = out[2] lens = len(coms); newLen = 0 print lens while newLen <> lens: newLen = clear () if newLen <> lens: lens = newLen newLen = 0 if bulls == 4: _win = 1 it += 1 print "\n[",it,"]" # for i in xrange(len(his)): # print his[i], hist[i][0], hist[i][1]