How to use hill_climb method in hypothesis

Best Python code snippet using hypothesis

trial_1.py

Source:trial_1.py Github

copy

Full Screen

...55# =============================================================================56#print("The initial key is",key_1)57 58test_list = []59def hill_climb(key_1):60# =============================================================================61# for i in range(len(alphabets_list)):62# 63# random_list = random.choice(alphabets_list)64# #random_list_1 = [i.split('\t')[0] for i in random_list] 65# print(random_list)66# =============================================================================67 dictionary = dict((key_1[i], alphabets_list[i]) for i in range(len(key_1)))68 #print(dictionary)69 #test_list = dictionary.keys()70 #print(test_list)71 test_list_1 = []72 dic_ini = []73 for i in range(len(cipher_list)):74 75 crypt = dictionary.get(cipher_list[i])76 dic_ini = np.append(dic_ini, crypt)77 #print(dic_ini)78 res_join = " ".join(str(x) for x in dic_ini)79 res_replace = res_join.replace(" ", "") 80 #print(res_replace)81 82 n = 383 temp = []84 out = [(res_replace[i:i+n]) for i in range(len(res_replace)+1 -n)]85 #print(out)86 temp = out87 #print(temp) 88 prob_k1 = 089 for i in range(len(temp)):90 res = dict_prob_3.get(temp[i])91 #res1 = np.sum(res)92 prob_k1 = res+ prob_k193 #prob_k1_list = np.append(prob_k1_list, res1)94 #print("The total prob of encrypted text is: ",add)95 #print("Prob using the key {} and the key is {}".format(prob_k1, "".join(str(x) for x in key_1)) )96 #print()97 return prob_k198 #return prob_k1, "".join(str(x) for x in random_list)99random_list = list(random_list)100random_list[0], random_list[1] = random_list[1], random_list[0]101key_2 = random_list102#print("The next key is ","".join(str(x) for x in key_2))103print("The prob using key 1 is {} and the key is {}".format(hill_climb(key_1) ,"".join(str(x) for x in key_1)))104print("The prob using key 2 is {} and the key is {}".format(hill_climb(key_2) ,"".join(str(x) for x in key_2)))105if(hill_climb(key_1) > hill_climb(key_2)):106 #print("true")107 key_1 = list(key_1)108 key_1[1], key_1[2] = key_1[2], key_1[1]109 key_3 = key_1110 print("The prob using key 3 is {} and the key is {}".format(hill_climb(key_3) ,"".join(str(x) for x in key_3)))111 hill_climb(key_3)112else:113 #print("false")114 key_2 = list(key_2)115 key_2[24], key_2[25] = key_2[25], key_2[24]116 key_3 = key_2117 print("The prob using key 3 is {} and the key is {}".format(hill_climb(key_3) ,"".join(str(x) for x in key_3)))118 hill_climb(key_3)119if(hill_climb(key_3) > hill_climb(key_2)):120 #print("true")121 key_3 = list(key_3)122 key_3[7], key_3[8] = key_3[8], key_3[7]123 key_4 = key_3124 print("The prob using key 4 is {} and the key is {}".format(hill_climb(key_4) ,"".join(str(x) for x in key_4)))125 hill_climb(key_4)126else:127 #print("false")128 key_2 = list(key_2)129 key_2[11], key_2[12] = key_2[12], key_2[11]130 key_4 = key_2131 print("The prob using key 4 is {} and the key is {}".format(hill_climb(key_4) ,"".join(str(x) for x in key_4)))132 hill_climb(key_4)133 134if(hill_climb(key_4) > hill_climb(key_3)):135 #print("true")136 key_4 = list(key_4)137 key_4[14], key_4[15] = key_4[15], key_4[14]138 key_5 = key_4139 print("The prob using key 5 is {} and the key is {}".format(hill_climb(key_5) ,"".join(str(x) for x in key_5)))140 hill_climb(key_5) 141else:142 #print("false")143 key_3 = list(key_3)144 key_3[18], key_3[19] = key_3[19], key_3[18]145 key_5 = key_3146 print("The prob using key 5 is {} and the key is {}".format(hill_climb(key_5) ,"".join(str(x) for x in key_5)))147 hill_climb(key_5) 148if(hill_climb(key_5) > hill_climb(key_4)):149 #print("true")150 key_5 = list(key_5)151 key_5[20], key_5[21] = key_5[21], key_5[20]152 key_6 = key_5153 print("The prob using key 6 is {} and the key is {}".format(hill_climb(key_6) ,"".join(str(x) for x in key_6)))154 hill_climb(key_6)155 156else:157 #print("false")158 key_4 = list(key_4)159 key_4[21], key_4[22] = key_4[22], key_4[21]160 key_6 = key_4161 print("The prob using key 6 is {} and the key is {}".format(hill_climb(key_6) ,"".join(str(x) for x in key_6)))162 hill_climb(key_6) 163if(hill_climb(key_6) < hill_climb(key_5)):164 165 key_5 = list(key_5)166 key_5[22], key_5[23] = key_5[23], key_5[22]167 key_7 = key_5168 print("The prob using key 7 is {} and the key is {}".format(hill_climb(key_7) ,"".join(str(x) for x in key_7)))169 hill_climb(key_7) 170 171 if(hill_climb(key_7) < hill_climb(key_5)):172 key_5 = list(key_5)173 key_5[3], key_5[4] = key_5[4], key_5[3]174 key_8 = key_5175 print("The prob using key 8 is {} and the key is {}".format(hill_climb(key_8) ,"".join(str(x) for x in key_8)))176 hill_climb(key_8) 177 178 if(hill_climb(key_8) < hill_climb(key_5)):179 key_5 = list(key_5)180 key_5[7], key_5[8] = key_5[8], key_5[7]181 key_9 = key_5182 print("The prob using key 9 is {} and the key is {}".format(hill_climb(key_9) ,"".join(str(x) for x in key_9)))183 hill_climb(key_9) 184 185 if(hill_climb(key_8) < hill_climb(key_5)):186 key_5 = list(key_5)187 key_5[20], key_5[21] = key_5[0], key_5[25]188 key_10 = key_5189 print("The prob using key 10 is {} and the key is {}".format(hill_climb(key_10) ,"".join(str(x) for x in key_10)))190 hill_climb(key_10) 191else:192 key_6 = list(key_6)193 key_6[22], key_6[23] = key_6[23], key_6[22]194 key_7 = key_6195 print("The prob using key 7 is {} and the key is {}".format(hill_climb(key_7) ,"".join(str(x) for x in key_7)))196 hill_climb(key_7) 197x1 = hill_climb(key_1)198x2 = hill_climb(key_2)199x3 = hill_climb(key_3)200x4 = hill_climb(key_4)201x5 = hill_climb(key_5)202x6 = hill_climb(key_6)203x7 = hill_climb(key_7)204y1 = "".join(str(x) for x in key_1)205y2 = "".join(str(x) for x in key_2)206y3 = "".join(str(x) for x in key_3)207y4 = "".join(str(x) for x in key_4)208y5 = "".join(str(x) for x in key_5)209y6 = "".join(str(x) for x in key_6)210y7 = "".join(str(x) for x in key_7)211test_list = x1,x2,x3,x4,x5,x6,x7212test_list_1 = y1,y2,y3,y4,y5,y6,y7213#print(test_list)214#print(test_list_1)215print()216dictionary1 = dict(zip(test_list_1, test_list))217#print(dictionary1)218print("The key with max prob is : {} and the value is : {} ".format(max(dictionary1, key=dictionary1.get),max([i for i in dictionary1.values()]) ))219#rint(max([i for i in dictionary1.values()]) )220# Create a dictionary from zip object221#dictOfWords = dict(dictionary1)222#print(dictOfWords)223y = max(dictionary1, key=dictionary1.get)224z = max([i for i in dictionary1.values()])225print(z, y)226print()227print("===========================Second iteration=============================")228def key(stringLength=26):229 """Generate a random string of fixed length """230 letters = string.ascii_lowercase231 return ''.join(random.sample(letters, stringLength))232random_list = key()233key_1 = random_list234print("The initial random key is",key_1)235print()236# =============================================================================237# for i in range(len(alphabets_list)):238# 239# random_list_1 = random.choice(random_list)240# #random_list_1 = [i.split('\t')[0] for i in random_list] 241# =============================================================================242#print("The initial key is",key_1)243 244test_list = []245def hill_climb(key_1):246# =============================================================================247# for i in range(len(alphabets_list)):248# 249# random_list = random.choice(alphabets_list)250# #random_list_1 = [i.split('\t')[0] for i in random_list] 251# print(random_list)252# =============================================================================253 dictionary = dict((key_1[i], alphabets_list[i]) for i in range(len(key_1)))254 #print(dictionary)255 #test_list = dictionary.keys()256 #print(test_list)257 test_list_1 = []258 dic_ini = []259 for i in range(len(cipher_list)):260 261 crypt = dictionary.get(cipher_list[i])262 dic_ini = np.append(dic_ini, crypt)263 #print(dic_ini)264 res_join = " ".join(str(x) for x in dic_ini)265 res_replace = res_join.replace(" ", "") 266 #print(res_replace)267 268 n = 3269 temp = []270 out = [(res_replace[i:i+n]) for i in range(len(res_replace)+1 -n)]271 #print(out)272 temp = out273 #print(temp) 274 prob_k1 = 0275 for i in range(len(temp)):276 res = dict_prob_3.get(temp[i])277 #res1 = np.sum(res)278 prob_k1 = res+ prob_k1279 #prob_k1_list = np.append(prob_k1_list, res1)280 #print("The total prob of encrypted text is: ",add)281 #print("Prob using the key {} and the key is {}".format(prob_k1, "".join(str(x) for x in key_1)) )282 #print()283 return prob_k1284 #return prob_k1, "".join(str(x) for x in random_list)285random_list = list(random_list)286random_list[0], random_list[1] = random_list[1], random_list[0]287key_2 = random_list288#print("The next key is ","".join(str(x) for x in key_2))289print("The prob using key 1 is {} and the key is {}".format(hill_climb(key_1) ,"".join(str(x) for x in key_1)))290print("The prob using key 2 is {} and the key is {}".format(hill_climb(key_2) ,"".join(str(x) for x in key_2)))291if(hill_climb(key_1) > hill_climb(key_2)):292 #print("true")293 key_1 = list(key_1)294 key_1[1], key_1[2] = key_1[2], key_1[1]295 key_3 = key_1296 print("The prob using key 3 is {} and the key is {}".format(hill_climb(key_3) ,"".join(str(x) for x in key_3)))297 hill_climb(key_3)298else:299 #print("false")300 key_2 = list(key_2)301 key_2[24], key_2[25] = key_2[25], key_2[24]302 key_3 = key_2303 print("The prob using key 3 is {} and the key is {}".format(hill_climb(key_3) ,"".join(str(x) for x in key_3)))304 hill_climb(key_3)305if(hill_climb(key_3) > hill_climb(key_2)):306 #print("true")307 key_3 = list(key_3)308 key_3[7], key_3[8] = key_3[8], key_3[7]309 key_4 = key_3310 print("The prob using key 4 is {} and the key is {}".format(hill_climb(key_4) ,"".join(str(x) for x in key_4)))311 hill_climb(key_4)312else:313 #print("false")314 key_2 = list(key_2)315 key_2[11], key_2[12] = key_2[12], key_2[11]316 key_4 = key_2317 print("The prob using key 4 is {} and the key is {}".format(hill_climb(key_4) ,"".join(str(x) for x in key_4)))318 hill_climb(key_4)319 320if(hill_climb(key_4) > hill_climb(key_3)):321 #print("true")322 key_4 = list(key_4)323 key_4[14], key_4[15] = key_4[15], key_4[14]324 key_5 = key_4325 print("The prob using key 5 is {} and the key is {}".format(hill_climb(key_5) ,"".join(str(x) for x in key_5)))326 hill_climb(key_5) 327else:328 #print("false")329 key_3 = list(key_3)330 key_3[18], key_3[19] = key_3[19], key_3[18]331 key_5 = key_3332 print("The prob using key 5 is {} and the key is {}".format(hill_climb(key_5) ,"".join(str(x) for x in key_5)))333 hill_climb(key_5) 334if(hill_climb(key_5) > hill_climb(key_4)):335 #print("true")336 key_5 = list(key_5)337 key_5[20], key_5[21] = key_5[21], key_5[20]338 key_6 = key_5339 print("The prob using key 6 is {} and the key is {}".format(hill_climb(key_6) ,"".join(str(x) for x in key_6)))340 hill_climb(key_6)341 342else:343 #print("false")344 key_4 = list(key_4)345 key_4[21], key_4[22] = key_4[22], key_4[21]346 key_6 = key_4347 print("The prob using key 6 is {} and the key is {}".format(hill_climb(key_6) ,"".join(str(x) for x in key_6)))348 hill_climb(key_6) 349if(hill_climb(key_6) < hill_climb(key_5)):350 351 key_5 = list(key_5)352 key_5[22], key_5[23] = key_5[23], key_5[22]353 key_7 = key_5354 print("The prob using key 7 is {} and the key is {}".format(hill_climb(key_7) ,"".join(str(x) for x in key_7)))355 hill_climb(key_7) 356 357 if(hill_climb(key_7) < hill_climb(key_5)):358 key_5 = list(key_5)359 key_5[3], key_5[4] = key_5[4], key_5[3]360 key_8 = key_5361 print("The prob using key 8 is {} and the key is {}".format(hill_climb(key_8) ,"".join(str(x) for x in key_8)))362 hill_climb(key_8) 363 364 if(hill_climb(key_8) < hill_climb(key_5)):365 key_5 = list(key_5)366 key_5[7], key_5[8] = key_5[8], key_5[7]367 key_9 = key_5368 print("The prob using key 9 is {} and the key is {}".format(hill_climb(key_9) ,"".join(str(x) for x in key_9)))369 hill_climb(key_9) 370 371 if(hill_climb(key_8) < hill_climb(key_5)):372 key_5 = list(key_5)373 key_5[20], key_5[21] = key_5[0], key_5[25]374 key_10 = key_5375 print("The prob using key 10 is {} and the key is {}".format(hill_climb(key_10) ,"".join(str(x) for x in key_10)))376 hill_climb(key_10) 377else:378 key_6 = list(key_6)379 key_6[22], key_6[23] = key_6[23], key_6[22]380 key_7 = key_6381 print("The prob using key 7 is {} and the key is {}".format(hill_climb(key_7) ,"".join(str(x) for x in key_7)))382 hill_climb(key_7) 383x1 = hill_climb(key_1)384x2 = hill_climb(key_2)385x3 = hill_climb(key_3)386x4 = hill_climb(key_4)387x5 = hill_climb(key_5)388x6 = hill_climb(key_6)389x7 = hill_climb(key_7)390y1 = "".join(str(x) for x in key_1)391y2 = "".join(str(x) for x in key_2)392y3 = "".join(str(x) for x in key_3)393y4 = "".join(str(x) for x in key_4)394y5 = "".join(str(x) for x in key_5)395y6 = "".join(str(x) for x in key_6)396y7 = "".join(str(x) for x in key_7)397test_list = x1,x2,x3,x4,x5,x6,x7398test_list_1 = y1,y2,y3,y4,y5,y6,y7399#print(test_list)400print()401dictionary1 = dict(zip(test_list_1, test_list))402#print(dictionary1)403print("The key with max prob is : {} and the value is : {} ".format(max(dictionary1, key=dictionary1.get),max([i for i in dictionary1.values()]) ))404#rint(max([i for i in dictionary1.values()]) )405# Create a dictionary from zip object406#dictOfWords = dict(dictionary1)407#print(dictOfWords)408print()409y = max(dictionary1, key=dictionary1.get)410z = max([i for i in dictionary1.values()])411print(z, y)412print("===========================Third iteration=============================")413def key(stringLength=26):414 """Generate a random string of fixed length """415 letters = string.ascii_lowercase416 return ''.join(random.sample(letters, stringLength))417random_list = key()418key_1 = random_list419print("The initial random key is",key_1)420print()421# =============================================================================422# for i in range(len(alphabets_list)):423# 424# random_list_1 = random.choice(random_list)425# #random_list_1 = [i.split('\t')[0] for i in random_list] 426# =============================================================================427#print("The initial key is",key_1)428 429test_list = []430def hill_climb(key_1):431# =============================================================================432# for i in range(len(alphabets_list)):433# 434# random_list = random.choice(alphabets_list)435# #random_list_1 = [i.split('\t')[0] for i in random_list] 436# print(random_list)437# =============================================================================438 dictionary = dict((key_1[i], alphabets_list[i]) for i in range(len(key_1)))439 #print(dictionary)440 #test_list = dictionary.keys()441 #print(test_list)442 test_list_1 = []443 dic_ini = []444 for i in range(len(cipher_list)):445 446 crypt = dictionary.get(cipher_list[i])447 dic_ini = np.append(dic_ini, crypt)448 #print(dic_ini)449 res_join = " ".join(str(x) for x in dic_ini)450 res_replace = res_join.replace(" ", "") 451 #print(res_replace)452 453 n = 3454 temp = []455 out = [(res_replace[i:i+n]) for i in range(len(res_replace)+1 -n)]456 #print(out)457 temp = out458 #print(temp) 459 prob_k1 = 0460 for i in range(len(temp)):461 res = dict_prob_3.get(temp[i])462 #res1 = np.sum(res)463 prob_k1 = res+ prob_k1464 #prob_k1_list = np.append(prob_k1_list, res1)465 #print("The total prob of encrypted text is: ",add)466 #print("Prob using the key {} and the key is {}".format(prob_k1, "".join(str(x) for x in key_1)) )467 #print()468 return prob_k1469 #return prob_k1, "".join(str(x) for x in random_list)470random_list = list(random_list)471random_list[0], random_list[1] = random_list[1], random_list[0]472key_2 = random_list473#print("The next key is ","".join(str(x) for x in key_2))474print("The prob using key 1 is {} and the key is {}".format(hill_climb(key_1) ,"".join(str(x) for x in key_1)))475print("The prob using key 2 is {} and the key is {}".format(hill_climb(key_2) ,"".join(str(x) for x in key_2)))476if(hill_climb(key_1) > hill_climb(key_2)):477 #print("true")478 key_1 = list(key_1)479 key_1[1], key_1[2] = key_1[2], key_1[1]480 key_3 = key_1481 print("The prob using key 3 is {} and the key is {}".format(hill_climb(key_3) ,"".join(str(x) for x in key_3)))482 hill_climb(key_3)483else:484 #print("false")485 key_2 = list(key_2)486 key_2[24], key_2[25] = key_2[25], key_2[24]487 key_3 = key_2488 print("The prob using key 3 is {} and the key is {}".format(hill_climb(key_3) ,"".join(str(x) for x in key_3)))489 hill_climb(key_3)490if(hill_climb(key_3) > hill_climb(key_2)):491 #print("true")492 key_3 = list(key_3)493 key_3[7], key_3[8] = key_3[8], key_3[7]494 key_4 = key_3495 print("The prob using key 4 is {} and the key is {}".format(hill_climb(key_4) ,"".join(str(x) for x in key_4)))496 hill_climb(key_4)497else:498 #print("false")499 key_2 = list(key_2)500 key_2[11], key_2[12] = key_2[12], key_2[11]501 key_4 = key_2502 print("The prob using key 4 is {} and the key is {}".format(hill_climb(key_4) ,"".join(str(x) for x in key_4)))503 hill_climb(key_4)504 505if(hill_climb(key_4) > hill_climb(key_3)):506 #print("true")507 key_4 = list(key_4)508 key_4[14], key_4[15] = key_4[15], key_4[14]509 key_5 = key_4510 print("The prob using key 5 is {} and the key is {}".format(hill_climb(key_5) ,"".join(str(x) for x in key_5)))511 hill_climb(key_5) 512else:513 #print("false")514 key_3 = list(key_3)515 key_3[18], key_3[19] = key_3[19], key_3[18]516 key_5 = key_3517 print("The prob using key 5 is {} and the key is {}".format(hill_climb(key_5) ,"".join(str(x) for x in key_5)))518 hill_climb(key_5) 519if(hill_climb(key_5) > hill_climb(key_4)):520 #print("true")521 key_5 = list(key_5)522 key_5[20], key_5[21] = key_5[21], key_5[20]523 key_6 = key_5524 print("The prob using key 6 is {} and the key is {}".format(hill_climb(key_6) ,"".join(str(x) for x in key_6)))525 hill_climb(key_6)526 527else:528 #print("false")529 key_4 = list(key_4)530 key_4[21], key_4[22] = key_4[22], key_4[21]531 key_6 = key_4532 print("The prob using key 6 is {} and the key is {}".format(hill_climb(key_6) ,"".join(str(x) for x in key_6)))533 hill_climb(key_6) 534if(hill_climb(key_6) < hill_climb(key_5)):535 536 key_5 = list(key_5)537 key_5[22], key_5[23] = key_5[23], key_5[22]538 key_7 = key_5539 print("The prob using key 7 is {} and the key is {}".format(hill_climb(key_7) ,"".join(str(x) for x in key_7)))540 hill_climb(key_7) 541 542 if(hill_climb(key_7) < hill_climb(key_5)):543 key_5 = list(key_5)544 key_5[3], key_5[4] = key_5[4], key_5[3]545 key_8 = key_5546 print("The prob using key 8 is {} and the key is {}".format(hill_climb(key_8) ,"".join(str(x) for x in key_8)))547 hill_climb(key_8) 548 549 if(hill_climb(key_8) < hill_climb(key_5)):550 key_5 = list(key_5)551 key_5[7], key_5[8] = key_5[8], key_5[7]552 key_9 = key_5553 print("The prob using key 9 is {} and the key is {}".format(hill_climb(key_9) ,"".join(str(x) for x in key_9)))554 hill_climb(key_9) 555 556 if(hill_climb(key_8) < hill_climb(key_5)):557 key_5 = list(key_5)558 key_5[20], key_5[21] = key_5[0], key_5[25]559 key_10 = key_5560 print("The prob using key 10 is {} and the key is {}".format(hill_climb(key_10) ,"".join(str(x) for x in key_10)))561 hill_climb(key_10) 562else:563 key_6 = list(key_6)564 key_6[22], key_6[23] = key_6[23], key_6[22]565 key_7 = key_6566 print("The prob using key 7 is {} and the key is {}".format(hill_climb(key_7) ,"".join(str(x) for x in key_7)))567 hill_climb(key_7) 568x1 = hill_climb(key_1)569x2 = hill_climb(key_2)570x3 = hill_climb(key_3)571x4 = hill_climb(key_4)572x5 = hill_climb(key_5)573x6 = hill_climb(key_6)574x7 = hill_climb(key_7)575y1 = "".join(str(x) for x in key_1)576y2 = "".join(str(x) for x in key_2)577y3 = "".join(str(x) for x in key_3)578y4 = "".join(str(x) for x in key_4)579y5 = "".join(str(x) for x in key_5)580y6 = "".join(str(x) for x in key_6)581y7 = "".join(str(x) for x in key_7)582test_list = x1,x2,x3,x4,x5,x6,x7583test_list_1 = y1,y2,y3,y4,y5,y6,y7584#print(test_list)585print()586dictionary1 = dict(zip(test_list_1, test_list))587#print(dictionary1)588print("The key with max prob is : {} and the value is : {} ".format(max(dictionary1, key=dictionary1.get),max([i for i in dictionary1.values()]) ))589#rint(max([i for i in dictionary1.values()]) )590# Create a dictionary from zip object591#dictOfWords = dict(dictionary1)592#print(dictOfWords)593print()594print("===========================Fourth iteration=============================")595def key(stringLength=26):596 """Generate a random string of fixed length """597 letters = string.ascii_lowercase598 return ''.join(random.sample(letters, stringLength))599random_list = key()600key_1 = random_list601print("The initial random key is",key_1)602print()603# =============================================================================604# for i in range(len(alphabets_list)):605# 606# random_list_1 = random.choice(random_list)607# #random_list_1 = [i.split('\t')[0] for i in random_list] 608# =============================================================================609#print("The initial key is",key_1)610 611test_list = []612def hill_climb(key_1):613# =============================================================================614# for i in range(len(alphabets_list)):615# 616# random_list = random.choice(alphabets_list)617# #random_list_1 = [i.split('\t')[0] for i in random_list] 618# print(random_list)619# =============================================================================620 dictionary = dict((key_1[i], alphabets_list[i]) for i in range(len(key_1)))621 #print(dictionary)622 #test_list = dictionary.keys()623 #print(test_list)624 test_list_1 = []625 dic_ini = []626 for i in range(len(cipher_list)):627 628 crypt = dictionary.get(cipher_list[i])629 dic_ini = np.append(dic_ini, crypt)630 #print(dic_ini)631 res_join = " ".join(str(x) for x in dic_ini)632 res_replace = res_join.replace(" ", "") 633 #print(res_replace)634 635 n = 3636 temp = []637 out = [(res_replace[i:i+n]) for i in range(len(res_replace)+1 -n)]638 #print(out)639 temp = out640 #print(temp) 641 prob_k1 = 0642 for i in range(len(temp)):643 res = dict_prob_3.get(temp[i])644 #res1 = np.sum(res)645 prob_k1 = res+ prob_k1646 #prob_k1_list = np.append(prob_k1_list, res1)647 #print("The total prob of encrypted text is: ",add)648 #print("Prob using the key {} and the key is {}".format(prob_k1, "".join(str(x) for x in key_1)) )649 #print()650 return prob_k1651 #return prob_k1, "".join(str(x) for x in random_list)652random_list = list(random_list)653random_list[0], random_list[1] = random_list[1], random_list[0]654key_2 = random_list655#print("The next key is ","".join(str(x) for x in key_2))656print("The prob using key 1 is {} and the key is {}".format(hill_climb(key_1) ,"".join(str(x) for x in key_1)))657print("The prob using key 2 is {} and the key is {}".format(hill_climb(key_2) ,"".join(str(x) for x in key_2)))658if(hill_climb(key_1) > hill_climb(key_2)):659 #print("true")660 key_1 = list(key_1)661 key_1[1], key_1[2] = key_1[2], key_1[1]662 key_3 = key_1663 print("The prob using key 3 is {} and the key is {}".format(hill_climb(key_3) ,"".join(str(x) for x in key_3)))664 hill_climb(key_3)665else:666 #print("false")667 key_2 = list(key_2)668 key_2[24], key_2[25] = key_2[25], key_2[24]669 key_3 = key_2670 print("The prob using key 3 is {} and the key is {}".format(hill_climb(key_3) ,"".join(str(x) for x in key_3)))671 hill_climb(key_3)672if(hill_climb(key_3) > hill_climb(key_2)):673 #print("true")674 key_3 = list(key_3)675 key_3[7], key_3[8] = key_3[8], key_3[7]676 key_4 = key_3677 print("The prob using key 4 is {} and the key is {}".format(hill_climb(key_4) ,"".join(str(x) for x in key_4)))678 hill_climb(key_4)679else:680 #print("false")681 key_2 = list(key_2)682 key_2[11], key_2[12] = key_2[12], key_2[11]683 key_4 = key_2684 print("The prob using key 4 is {} and the key is {}".format(hill_climb(key_4) ,"".join(str(x) for x in key_4)))685 hill_climb(key_4)686 687if(hill_climb(key_4) > hill_climb(key_3)):688 #print("true")689 key_4 = list(key_4)690 key_4[14], key_4[15] = key_4[15], key_4[14]691 key_5 = key_4692 print("The prob using key 5 is {} and the key is {}".format(hill_climb(key_5) ,"".join(str(x) for x in key_5)))693 hill_climb(key_5) 694else:695 #print("false")696 key_3 = list(key_3)697 key_3[18], key_3[19] = key_3[19], key_3[18]698 key_5 = key_3699 print("The prob using key 5 is {} and the key is {}".format(hill_climb(key_5) ,"".join(str(x) for x in key_5)))700 hill_climb(key_5) 701if(hill_climb(key_5) > hill_climb(key_4)):702 #print("true")703 key_5 = list(key_5)704 key_5[20], key_5[21] = key_5[21], key_5[20]705 key_6 = key_5706 print("The prob using key 6 is {} and the key is {}".format(hill_climb(key_6) ,"".join(str(x) for x in key_6)))707 hill_climb(key_6)708 709else:710 #print("false")711 key_4 = list(key_4)712 key_4[21], key_4[22] = key_4[22], key_4[21]713 key_6 = key_4714 print("The prob using key 6 is {} and the key is {}".format(hill_climb(key_6) ,"".join(str(x) for x in key_6)))715 hill_climb(key_6) 716if(hill_climb(key_6) < hill_climb(key_5)):717 718 key_5 = list(key_5)719 key_5[22], key_5[23] = key_5[23], key_5[22]720 key_7 = key_5721 print("The prob using key 7 is {} and the key is {}".format(hill_climb(key_7) ,"".join(str(x) for x in key_7)))722 hill_climb(key_7) 723 724 if(hill_climb(key_7) < hill_climb(key_5)):725 key_5 = list(key_5)726 key_5[3], key_5[4] = key_5[4], key_5[3]727 key_8 = key_5728 print("The prob using key 8 is {} and the key is {}".format(hill_climb(key_8) ,"".join(str(x) for x in key_8)))729 hill_climb(key_8) 730 731 if(hill_climb(key_8) < hill_climb(key_5)):732 key_5 = list(key_5)733 key_5[7], key_5[8] = key_5[8], key_5[7]734 key_9 = key_5735 print("The prob using key 9 is {} and the key is {}".format(hill_climb(key_9) ,"".join(str(x) for x in key_9)))736 hill_climb(key_9) 737 738 if(hill_climb(key_8) < hill_climb(key_5)):739 key_5 = list(key_5)740 key_5[20], key_5[21] = key_5[0], key_5[25]741 key_10 = key_5742 print("The prob using key 10 is {} and the key is {}".format(hill_climb(key_10) ,"".join(str(x) for x in key_10)))743 hill_climb(key_10) 744else:745 key_6 = list(key_6)746 key_6[22], key_6[23] = key_6[23], key_6[22]747 key_7 = key_6748 print("The prob using key 7 is {} and the key is {}".format(hill_climb(key_7) ,"".join(str(x) for x in key_7)))749 hill_climb(key_7) 750x1 = hill_climb(key_1)751x2 = hill_climb(key_2)752x3 = hill_climb(key_3)753x4 = hill_climb(key_4)754x5 = hill_climb(key_5)755x6 = hill_climb(key_6)756x7 = hill_climb(key_7)757y1 = "".join(str(x) for x in key_1)758y2 = "".join(str(x) for x in key_2)759y3 = "".join(str(x) for x in key_3)760y4 = "".join(str(x) for x in key_4)761y5 = "".join(str(x) for x in key_5)762y6 = "".join(str(x) for x in key_6)763y7 = "".join(str(x) for x in key_7)764test_list = x1,x2,x3,x4,x5,x6,x7765test_list_1 = y1,y2,y3,y4,y5,y6,y7766#print(test_list)767print()768dictionary1 = dict(zip(test_list_1, test_list))769#print(dictionary1)770print("The key with max prob is : {} and the value is : {} ".format(max(dictionary1, key=dictionary1.get),max([i for i in dictionary1.values()]) ))771#rint(max([i for i in dictionary1.values()]) )772# Create a dictionary from zip object773#dictOfWords = dict(dictionary1)774#print(dictOfWords)775print()776print("===========================Fifth iteration=============================")777def key(stringLength=26):778 """Generate a random string of fixed length """779 letters = string.ascii_lowercase780 return ''.join(random.sample(letters, stringLength))781random_list = key()782key_1 = random_list783print("The initial random key is",key_1)784print()785# =============================================================================786# for i in range(len(alphabets_list)):787# 788# random_list_1 = random.choice(random_list)789# #random_list_1 = [i.split('\t')[0] for i in random_list] 790# =============================================================================791#print("The initial key is",key_1)792 793test_list = []794def hill_climb(key_1):795# =============================================================================796# for i in range(len(alphabets_list)):797# 798# random_list = random.choice(alphabets_list)799# #random_list_1 = [i.split('\t')[0] for i in random_list] 800# print(random_list)801# =============================================================================802 dictionary = dict((key_1[i], alphabets_list[i]) for i in range(len(key_1)))803 #print(dictionary)804 #test_list = dictionary.keys()805 #print(test_list)806 test_list_1 = []807 dic_ini = []808 for i in range(len(cipher_list)):809 810 crypt = dictionary.get(cipher_list[i])811 dic_ini = np.append(dic_ini, crypt)812 #print(dic_ini)813 res_join = " ".join(str(x) for x in dic_ini)814 res_replace = res_join.replace(" ", "") 815 #print(res_replace)816 817 n = 3818 temp = []819 out = [(res_replace[i:i+n]) for i in range(len(res_replace)+1 -n)]820 #print(out)821 temp = out822 #print(temp) 823 prob_k1 = 0824 for i in range(len(temp)):825 res = dict_prob_3.get(temp[i])826 #res1 = np.sum(res)827 prob_k1 = res+ prob_k1828 #prob_k1_list = np.append(prob_k1_list, res1)829 #print("The total prob of encrypted text is: ",add)830 #print("Prob using the key {} and the key is {}".format(prob_k1, "".join(str(x) for x in key_1)) )831 #print()832 return prob_k1833 #return prob_k1, "".join(str(x) for x in random_list)834random_list = list(random_list)835random_list[0], random_list[1] = random_list[1], random_list[0]836key_2 = random_list837#print("The next key is ","".join(str(x) for x in key_2))838print("The prob using key 1 is {} and the key is {}".format(hill_climb(key_1) ,"".join(str(x) for x in key_1)))839print("The prob using key 2 is {} and the key is {}".format(hill_climb(key_2) ,"".join(str(x) for x in key_2)))840if(hill_climb(key_1) > hill_climb(key_2)):841 #print("true")842 key_1 = list(key_1)843 key_1[1], key_1[2] = key_1[2], key_1[1]844 key_3 = key_1845 print("The prob using key 3 is {} and the key is {}".format(hill_climb(key_3) ,"".join(str(x) for x in key_3)))846 hill_climb(key_3)847else:848 #print("false")849 key_2 = list(key_2)850 key_2[24], key_2[25] = key_2[25], key_2[24]851 key_3 = key_2852 print("The prob using key 3 is {} and the key is {}".format(hill_climb(key_3) ,"".join(str(x) for x in key_3)))853 hill_climb(key_3)854if(hill_climb(key_3) > hill_climb(key_2)):855 #print("true")856 key_3 = list(key_3)857 key_3[7], key_3[8] = key_3[8], key_3[7]858 key_4 = key_3859 print("The prob using key 4 is {} and the key is {}".format(hill_climb(key_4) ,"".join(str(x) for x in key_4)))860 hill_climb(key_4)861else:862 #print("false")863 key_2 = list(key_2)864 key_2[11], key_2[12] = key_2[12], key_2[11]865 key_4 = key_2866 print("The prob using key 4 is {} and the key is {}".format(hill_climb(key_4) ,"".join(str(x) for x in key_4)))867 hill_climb(key_4)868 869if(hill_climb(key_4) > hill_climb(key_3)):870 #print("true")871 key_4 = list(key_4)872 key_4[14], key_4[15] = key_4[15], key_4[14]873 key_5 = key_4874 print("The prob using key 5 is {} and the key is {}".format(hill_climb(key_5) ,"".join(str(x) for x in key_5)))875 hill_climb(key_5) 876else:877 #print("false")878 key_3 = list(key_3)879 key_3[18], key_3[19] = key_3[19], key_3[18]880 key_5 = key_3881 print("The prob using key 5 is {} and the key is {}".format(hill_climb(key_5) ,"".join(str(x) for x in key_5)))882 hill_climb(key_5) 883if(hill_climb(key_5) > hill_climb(key_4)):884 #print("true")885 key_5 = list(key_5)886 key_5[20], key_5[21] = key_5[21], key_5[20]887 key_6 = key_5888 print("The prob using key 6 is {} and the key is {}".format(hill_climb(key_6) ,"".join(str(x) for x in key_6)))889 hill_climb(key_6)890 891else:892 #print("false")893 key_4 = list(key_4)894 key_4[21], key_4[22] = key_4[22], key_4[21]895 key_6 = key_4896 print("The prob using key 6 is {} and the key is {}".format(hill_climb(key_6) ,"".join(str(x) for x in key_6)))897 hill_climb(key_6) 898if(hill_climb(key_6) < hill_climb(key_5)):899 900 key_5 = list(key_5)901 key_5[22], key_5[23] = key_5[23], key_5[22]902 key_7 = key_5903 print("The prob using key 7 is {} and the key is {}".format(hill_climb(key_7) ,"".join(str(x) for x in key_7)))904 hill_climb(key_7) 905 906 if(hill_climb(key_7) < hill_climb(key_5)):907 key_5 = list(key_5)908 key_5[3], key_5[4] = key_5[4], key_5[3]909 key_8 = key_5910 print("The prob using key 8 is {} and the key is {}".format(hill_climb(key_8) ,"".join(str(x) for x in key_8)))911 hill_climb(key_8) 912 913 if(hill_climb(key_8) < hill_climb(key_5)):914 key_5 = list(key_5)915 key_5[7], key_5[8] = key_5[8], key_5[7]916 key_9 = key_5917 print("The prob using key 9 is {} and the key is {}".format(hill_climb(key_9) ,"".join(str(x) for x in key_9)))918 hill_climb(key_9) 919 920 if(hill_climb(key_8) < hill_climb(key_5)):921 key_5 = list(key_5)922 key_5[20], key_5[21] = key_5[0], key_5[25]923 key_10 = key_5924 print("The prob using key 10 is {} and the key is {}".format(hill_climb(key_10) ,"".join(str(x) for x in key_10)))925 hill_climb(key_10) 926else:927 key_6 = list(key_6)928 key_6[22], key_6[23] = key_6[23], key_6[22]929 key_7 = key_6930 print("The prob using key 7 is {} and the key is {}".format(hill_climb(key_7) ,"".join(str(x) for x in key_7)))931 hill_climb(key_7) 932x1 = hill_climb(key_1)933x2 = hill_climb(key_2)934x3 = hill_climb(key_3)935x4 = hill_climb(key_4)936x5 = hill_climb(key_5)937x6 = hill_climb(key_6)938x7 = hill_climb(key_7)939y1 = "".join(str(x) for x in key_1)940y2 = "".join(str(x) for x in key_2)941y3 = "".join(str(x) for x in key_3)942y4 = "".join(str(x) for x in key_4)943y5 = "".join(str(x) for x in key_5)944y6 = "".join(str(x) for x in key_6)945y7 = "".join(str(x) for x in key_7)946test_list = x1,x2,x3,x4,x5,x6,x7947test_list_1 = y1,y2,y3,y4,y5,y6,y7948#print(test_list)949print()950dictionary1 = dict(zip(test_list_1, test_list))951#print(dictionary1)952print("The key with max prob is : {} and the value is : {} ".format(max(dictionary1, key=dictionary1.get),max([i for i in dictionary1.values()]) ))...

Full Screen

Full Screen

test_algorithms.py

Source:test_algorithms.py Github

copy

Full Screen

...11 @staticmethod12 def test_hill_climb_discrete_max():13 """Test hill_climb function for a discrete maximization problem"""14 problem = DiscreteOpt(5, OneMax(), maximize=True)15 best_state, best_fitness = hill_climb(problem, restarts=20)16 x = np.array([1, 1, 1, 1, 1])17 assert (np.array_equal(best_state, x) and best_fitness == 5)18 @staticmethod19 def test_hill_climb_continuous_max():20 """Test hill_climb function for a continuous maximization problem"""21 problem = ContinuousOpt(5, OneMax(), maximize=True)22 best_state, best_fitness = hill_climb(problem, restarts=20)23 x = np.array([1, 1, 1, 1, 1])24 assert (np.array_equal(best_state, x) and best_fitness == 5)25 @staticmethod26 def test_hill_climb_discrete_min():27 """Test hill_climb function for a discrete minimization problem"""28 problem = DiscreteOpt(5, OneMax(), maximize=False)29 best_state, best_fitness = hill_climb(problem, restarts=20)30 x = np.array([0, 0, 0, 0, 0])31 assert (np.array_equal(best_state, x) and best_fitness == 0)32 @staticmethod33 def test_hill_climb_continuous_min():34 """Test hill_climb function for a continuous minimization problem"""35 problem = ContinuousOpt(5, OneMax(), maximize=False)36 best_state, best_fitness = hill_climb(problem, restarts=20)37 x = np.array([0, 0, 0, 0, 0])38 assert (np.array_equal(best_state, x) and best_fitness == 0)39 @staticmethod40 def test_hill_climb_max_iters():41 """Test hill_climb function with max_iters less than infinite"""42 problem = DiscreteOpt(5, OneMax(), maximize=True)43 x = np.array([0, 0, 0, 0, 0])44 best_state, best_fitness = hill_climb(problem, max_iters=1,45 restarts=0, init_state=x)46 assert best_fitness == 147 @staticmethod48 def test_random_hill_climb_discrete_max():49 """Test random_hill_climb function for a discrete maximization50 problem"""51 problem = DiscreteOpt(5, OneMax(), maximize=True)52 best_state, best_fitness = random_hill_climb(problem, max_attempts=10,53 restarts=20)54 x = np.array([1, 1, 1, 1, 1])55 assert (np.array_equal(best_state, x) and best_fitness == 5)56 @staticmethod57 def test_random_hill_climb_continuous_max():58 """Test random_hill_climb function for a continuous maximization59 problem"""60 problem = ContinuousOpt(5, OneMax(), maximize=True)61 best_state, best_fitness = random_hill_climb(problem, max_attempts=10,62 restarts=20)63 x = np.array([1, 1, 1, 1, 1])64 assert (np.array_equal(best_state, x) and best_fitness == 5)65 @staticmethod66 def test_random_hill_climb_discrete_min():67 """Test random_hill_climb function for a discrete minimization68 problem"""69 problem = DiscreteOpt(5, OneMax(), maximize=False)70 best_state, best_fitness = random_hill_climb(problem, max_attempts=10,71 restarts=20)72 x = np.array([0, 0, 0, 0, 0])73 assert (np.array_equal(best_state, x) and best_fitness == 0)74 @staticmethod75 def test_random_hill_climb_continuous_min():76 """Test random_hill_climb function for a continuous minimization77 problem"""78 problem = ContinuousOpt(5, OneMax(), maximize=False)79 best_state, best_fitness = random_hill_climb(problem, max_attempts=10,80 restarts=20)81 x = np.array([0, 0, 0, 0, 0])82 assert (np.array_equal(best_state, x) and best_fitness == 0)83 @staticmethod84 def test_random_hill_climb_max_iters():85 """Test random_hill_climb function with max_iters less than infinite"""86 problem = DiscreteOpt(5, OneMax(), maximize=True)87 x = np.array([0, 0, 0, 0, 0])88 best_state, best_fitness = random_hill_climb(problem, max_attempts=1,89 max_iters=1, restarts=0,90 init_state=x)91 assert best_fitness == 192 @staticmethod93 def test_simulated_annealing_discrete_max():94 """Test simulated_annealing function for a discrete maximization95 problem"""96 problem = DiscreteOpt(5, OneMax(), maximize=True)97 best_state, best_fitness = simulated_annealing(problem,98 max_attempts=50)99 x = np.array([1, 1, 1, 1, 1])100 assert (np.array_equal(best_state, x) and best_fitness == 5)101 @staticmethod102 def test_simulated_annealing_continuous_max():...

Full Screen

Full Screen

main.py

Source:main.py Github

copy

Full Screen

1import random2import matplotlib.pyplot as plt3from HillClimb import HillClimb4from SimulatedAnnealing import SimulatedAnnealing5from data import DataModel6data_model = DataModel()7def init_random_tour():8 tour = list(range(len(data_model.data.get("cities"))))9 random.shuffle(tour)10 return tour11initial_tour = init_random_tour()12hill_climb = HillClimb(initial_tour, data_model)13print("Starting permutation", str(initial_tour), "\n")14#Hill climb15best_tour, best_distance, climb = hill_climb.perform_algorithm(data_model.reversed_sections)16print('Best permuttation for reversed method (Hill Climb):', str(best_tour),'Distance:', str(best_distance), '\n')17plt.plot(climb, color = "blue", label="Hill Climb (Reversing method)")18plt.ylabel('Distance')19plt.legend(loc="lower right")20best_tour, best_distance, climb = hill_climb.perform_algorithm(data_model.swapped_cities)21print('Best permuttation for swapping method (Hill Climb):', str(best_tour),'Distance:', str(best_distance), '\n')22plt.plot(climb, color = "green", label="Hill Climb (Swapping method)")23plt.legend(loc="lower right")24# Simulated annealing25simulated_annealing = SimulatedAnnealing(initial_tour, data_model)26print('Starting temperature:', simulated_annealing.start_temp )27best_tour, best_distance, climb = simulated_annealing.perform_algorithm(data_model.reversed_sections)28print('Best permuttation for reversed method (Simulated Annealing):', str(best_tour),'Distance:', str(best_distance), '\n')29plt.plot(climb, color = "red", label="Simulated Annealing (Reversing method)")30plt.legend(loc="lower right")31best_tour, best_distance, climb = simulated_annealing.perform_algorithm(data_model.swapped_cities)32print('Best permuttation for swapping method (Simulated Annealing):', str(best_tour),'Distance:', str(best_distance), '\n')33plt.plot(climb, color = "pink", label="Simulated Annealing (Swapping method)")34plt.legend(loc="lower right")...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful