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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| from random import randint as ri
def feedback(num_a, num_b): str_a = str(num_a) str_b = str(num_b) c = 0 e = 0 if len(str_a) == 3: str_a = "0" + str_a if len(str_b) == 3: str_b = "0" + str_b for i in range(4): for j in range(4): if str_a[i] == str_b[j]: c += 1 if i == j: e += 1 return (c, e)
def update_set(list_old, last, feed): list_new = [] num = len(list_old) for i in range(num): if feedback(list_old[i], last) == feed: list_new.append(list_old[i]) return list_new
def decision_1(list_new): choise = 0 for n in list_new: min_n = 10000 dict_f = {} for m in list_new: feed = feedback(n, m) if feed not in dict_f: dict_f[feed] = 1 else: dict_f[feed] += 1 temp = sum(dict_f.values()) / len(dict_f) if min_n > temp: min_n = temp choise = n return choise
def decision_2(list_new): choise = 0 for n in list_new: min_n = 10000 dict_f = {} for m in list_new: feed = feedback(n, m) if feed not in dict_f: dict_f[feed] = 1 else: dict_f[feed] += 1 temp = max(dict_f.values()) if min_n > temp: min_n = temp choise = n return choise
list_ini = [] for i1 in range(10): for i2 in range(10): if i1 == i2: continue for i3 in range(10): if i3 == i1 or i3 == i2: continue for i4 in range(10): if i4 == i1 or i4 == i2 or i4 == i3: continue list_ini.append(i1 * 1000 + i2 * 100 + i3 * 10 + i4) n = 10 * 9 * 8 * 7
sum_1 = 0 sum_2 = 0 t = 10000 dict_choise = {(0, 0): 9876, (1, 0): 9872, (1, 1): 9873, (2, 0): 9832, (2, 1): 9821, (2, 2): 9823, (3, 0): 9312, (3, 1): 9321, (3, 2): 9123, (3, 3): 9023, (4, 0): 3210, (4, 1): 3120, (4, 2): 3021, (4, 3): 0, (4, 4): 1023}
for j in range(t): ans_index = ri(0, n - 1) ans = list_ini[ans_index] i = 0 feed_ini = feedback(ans, 1023) list_one = update_set(list_ini, 1023, feed_ini) list_new = list_one for i in range(1, 50): if i == 1: choise = dict_choise[feed_ini] feed = feedback(ans, choise) if feed[1] == 4: i += 1 break list_new = update_set(list_new, choise, feed) choise = decision_1(list_new) sum_1 += i list_new = list_one i = 0 for i in range(1, 50): if i == 1: choise = dict_choise[feed_ini] feed = feedback(ans, choise) if feed[1] == 4: i += 1 break list_new = update_set(list_new, choise, feed) choise = decision_2(list_new) sum_2 += i print(j) print(sum_1 / t) print(sum_2 / t)
|