|
| 1 | +from itertools import permutations |
| 2 | + |
| 3 | +op_list = ["+", "-", "*", "/"] |
| 4 | + |
| 5 | +def get_input(): |
| 6 | + card_1 = int(input("first card point:\n")) |
| 7 | + card_2 = int(input("second card point:\n")) |
| 8 | + card_3 = int(input("third card point:\n")) |
| 9 | + card_4 = int(input("fourth card point:\n")) |
| 10 | + return [card_1, card_2, card_3, card_4] |
| 11 | + |
| 12 | +''' |
| 13 | +get_input() |
| 14 | +''' |
| 15 | + |
| 16 | +def is_24(val): |
| 17 | + if(abs(val-24)<0.000001): |
| 18 | + return 1 |
| 19 | + else: |
| 20 | + return 0 |
| 21 | + |
| 22 | +def cal_one_step(a=1, b=1, op="+", order=1): |
| 23 | + if(op=="+"): |
| 24 | + return a+b |
| 25 | + elif(op=="*"): |
| 26 | + return a*b |
| 27 | + elif(op=="-"): |
| 28 | + if order: |
| 29 | + return a-b |
| 30 | + else: |
| 31 | + return b-a |
| 32 | + elif(op=="/"): |
| 33 | + if order: |
| 34 | + if b==0: |
| 35 | + return 1000000 |
| 36 | + return a*1.0/b*1.0 |
| 37 | + else: |
| 38 | + if a==0: |
| 39 | + return 1000000 |
| 40 | + return b*1.0/a*1.0 |
| 41 | + else: |
| 42 | + raise |
| 43 | + |
| 44 | +def cal_two_all_op_result(a, b): |
| 45 | + all_result = [] |
| 46 | + op_record = [] |
| 47 | + for op in op_list: |
| 48 | + for order in [0,1]: |
| 49 | + all_result.append(cal_one_step(a,b,op,order)) |
| 50 | + op_record.append([op, order]) |
| 51 | + return all_result, op_record |
| 52 | + |
| 53 | +''' |
| 54 | +print(cal_two_all_op_result(1, 7)) |
| 55 | +''' |
| 56 | + |
| 57 | +def reorder_card(card_list): |
| 58 | + all_com = [] |
| 59 | + if(len(card_list) != 4): |
| 60 | + raise |
| 61 | + else: |
| 62 | + return list(permutations(card_list)) |
| 63 | + |
| 64 | +''' |
| 65 | +card_list = get_input() |
| 66 | +print(reorder_card(card_list)) |
| 67 | +print(len(reorder_card(card_list))) |
| 68 | +''' |
| 69 | + |
| 70 | +def cal_order_op_result(card_tp): |
| 71 | + ''' |
| 72 | + 第一种情况是1,2运算完之后的结果与3进行计算的结果,再与4计算 |
| 73 | + 第二种情况是1,2运算的结果与3,4运算的结果计算 |
| 74 | + ''' |
| 75 | + #case 1 |
| 76 | + first_result, first_op = cal_two_all_op_result(card_tp[0], card_tp[1]) |
| 77 | + |
| 78 | + #print(first_result) |
| 79 | + #print(first_op) |
| 80 | + |
| 81 | + second_result = [] |
| 82 | + second_op = [] |
| 83 | + for item in range(len(first_result)): |
| 84 | + tmp_result, tmp_op = cal_two_all_op_result(first_result[item], card_tp[2]) |
| 85 | + second_result.extend(tmp_result) |
| 86 | + third_result = [] |
| 87 | + third_op = [] |
| 88 | + for item in second_result: |
| 89 | + tmp_result, tmp_op = cal_two_all_op_result(item, card_tp[3]) |
| 90 | + third_result.extend(tmp_result) |
| 91 | + #print(third_result) |
| 92 | + #case 2 |
| 93 | + first_result, first_op = cal_two_all_op_result(card_tp[0], card_tp[1]) |
| 94 | + second_result, second_op = cal_two_all_op_result(card_tp[2], card_tp[3]) |
| 95 | + for fitem in first_result: |
| 96 | + for sitem in second_result: |
| 97 | + tmp_result, third_op = cal_two_all_op_result(fitem, sitem) |
| 98 | + third_result.extend(tmp_result) |
| 99 | + #print(third_result) |
| 100 | + #print(len(third_result)) |
| 101 | + op_index=[] |
| 102 | + for cnt in range(len(third_result)): |
| 103 | + if(is_24(third_result[cnt])): |
| 104 | + op_index.append(cnt) |
| 105 | + return op_index |
| 106 | + |
| 107 | +def trans_op_cnt(op_cnt): |
| 108 | + return [op_list[op_cnt//2], op_cnt%2] |
| 109 | + |
| 110 | +''' |
| 111 | +for i in range(8): |
| 112 | + print(trans_op_cnt(i)) |
| 113 | +''' |
| 114 | + |
| 115 | +def get_op_steps(op_cnt): |
| 116 | + op_case = op_cnt//512; |
| 117 | + op_cnt = op_cnt%512; |
| 118 | + step_num = [op_case, op_cnt//64, (op_cnt//8)%8, op_cnt%8] |
| 119 | + step_sig = [] |
| 120 | + step_sig.append(step_num[0]) |
| 121 | + for i in range(1,4): |
| 122 | + #print(trans_op_cnt(step_num[i])) |
| 123 | + step_sig.append(trans_op_cnt(step_num[i])) |
| 124 | + return step_num, step_sig |
| 125 | + |
| 126 | + |
| 127 | +card_list = get_input() |
| 128 | +all_result = [] |
| 129 | +#print(reorder_card(card_list)) |
| 130 | +#print(len(reorder_card(card_list))) |
| 131 | +#card_inst = (4,5,8,2) |
| 132 | +all_card_list = reorder_card(card_list) |
| 133 | +#all_card_list = [(2,8,5,4)] |
| 134 | +for card_inst in all_card_list: |
| 135 | + #print(card_inst) |
| 136 | + op_index_list = cal_order_op_result(card_inst) |
| 137 | + #print(op_index_list) |
| 138 | + if(len(op_index_list)>0): |
| 139 | + for item in op_index_list: |
| 140 | + cal_expr="" |
| 141 | + step_num, step_sig = get_op_steps(item) |
| 142 | + #print(step_num, step_sig) |
| 143 | + cal_expr = str(card_inst[0]) |
| 144 | + if(step_sig[1][1]): |
| 145 | + cal_expr = "(" + cal_expr + step_sig[1][0] + \ |
| 146 | + str(card_inst[1]) + ")" |
| 147 | + else: |
| 148 | + cal_expr = "(" + str(card_inst[1]) + step_sig[1][0] + \ |
| 149 | + cal_expr + ")" |
| 150 | + if(step_sig[0]==0): |
| 151 | + if(step_sig[2][1]): |
| 152 | + cal_expr = "(" + cal_expr + step_sig[2][0] + \ |
| 153 | + str(card_inst[2]) + ")" |
| 154 | + else: |
| 155 | + cal_expr = "(" + str(card_inst[2]) + step_sig[2][0] + \ |
| 156 | + cal_expr + ")" |
| 157 | + if(step_sig[3][1]): |
| 158 | + cal_expr = "(" + cal_expr + step_sig[3][0] + \ |
| 159 | + str(card_inst[3]) + ")" |
| 160 | + else: |
| 161 | + cal_expr = "(" + str(card_inst[3]) + step_sig[3][0] + \ |
| 162 | + cal_expr + ")" |
| 163 | + else: |
| 164 | + if(step_sig[2][1]): |
| 165 | + cal_expr_tmp = "(" + str(card_inst[2]) + step_sig[2][0] + \ |
| 166 | + str(card_inst[3]) + ")" |
| 167 | + else: |
| 168 | + cal_expr_tmp = "(" + str(card_inst[3]) + step_sig[2][0] + \ |
| 169 | + str(card_inst[2]) + ")" |
| 170 | + if(step_sig[3][1]): |
| 171 | + cal_expr = "(" + cal_expr + step_sig[3][0] + \ |
| 172 | + cal_expr_tmp + ")" |
| 173 | + else: |
| 174 | + cal_expr = "(" + cal_expr_tmp + step_sig[3][0] + \ |
| 175 | + cal_expr + ")" |
| 176 | + cal_expr = cal_expr + "=24" |
| 177 | + #print(step_sig[0]) |
| 178 | + #print(cal_expr) |
| 179 | + all_result.append(cal_expr) |
| 180 | + |
| 181 | +all_result_new = list(set(all_result)) |
| 182 | +if(len(all_result_new)>0): |
| 183 | + for item in all_result_new: |
| 184 | + #print(all_result_new) |
| 185 | + print(item) |
| 186 | +else: |
| 187 | + print("I can not calculate to 24 point, can you tell me how to do it?") |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + |
| 212 | + |
0 commit comments