def gen_patterns_within_distance_d(pattern, d): """ This helper function is used to generate all patterns within a Hamming distance d of the pattern given by pattern. Input: A string, pattern, and an integer, d (the Hamming distance). Output: A list of all patterns within Hamming distance d of the given pattern. """ alphabet = ['A', 'C', 'G', 'T'] # patlist = [ pattern ] # for indx in range(len(pattern)): # for nt in alphabet: # newpat = pattern[:i] + nt + pattern[i+1:] # patlist.append(newpat) allpatlist = gen_list_of_patterns(alphabet, len(pattern)) patlist = [] for val in allpatlist: hamdist = hamming.hamming(pattern, val) if hamdist <= d: patlist.append(val) #print(patlist) return (patlist)
def apc(text, pattern, d): count = 0 for i in range( len(text) - len(pattern) + 1 ): pattern1 = text[i:i+len(pattern)] if hamming(pattern, pattern1) <= d: count += 1 return count
def gen_patterns_within_distance_d(pattern, d): """ This helper function is used to generate all patterns within a Hamming distance d of the pattern given by pattern. Input: A string, pattern, and an integer, d (the Hamming distance). Output: A list of all patterns within Hamming distance d of the given pattern. """ alphabet = ['A', 'C', 'G', 'T'] # patlist = [ pattern ] # for indx in range(len(pattern)): # for nt in alphabet: # newpat = pattern[:i] + nt + pattern[i+1:] # patlist.append(newpat) allpatlist = gen_list_of_patterns(alphabet, len(pattern)) patlist = [] for val in allpatlist: hamdist = hamming.hamming(pattern, val) if hamdist <= d: patlist.append(val) #print(patlist) return(patlist)
def find(text, pattern, hamming_distance=0): pos = [] n = len(text) k = len(pattern) for i in range(0, n - k + 1): if hamming.hamming(text[i:i + k], pattern) <= hamming_distance: pos.append(i) return pos
def patterncount(text, pattern, d): count = 0 k = len(pattern) n = len(text) for i in range(0, n - k + 1): if hamming.hamming(text[i:i+k], pattern) <= d: count += 1 return count
def build_bolck_matrix(self, game_block_list): standard_start_point, standard_end_point = game_block_list[0] # +1 是考虑还有一条黑边框 standard_width = standard_end_point[0] - standard_start_point[0] + 1 standard_height = standard_end_point[1] - standard_start_point[1] + 1 cut_left = self.width cut_top = self.height for block in game_block_list: cut_left = min(cut_left, block[0][0]) cut_top = min(cut_top, block[0][1]) block_matrix = [[0 for j in range(12)] for i in range(16)] self.image_matrix = [[0 for j in range(12)] for i in range(16)] for block in game_block_list: start_point, end_point = block postion_x = int(float(start_point[0] - cut_left) / standard_width + 0.5) + 1 postion_y = int(float(start_point[1] - cut_top) / standard_height + 0.5) + 1 if not block_matrix[postion_x][postion_y] == 0: raise Exception # 非常给力的优化 切掉图像边上的一个像素的无用边 self.image_matrix[postion_x][postion_y] = self.im.crop( map(lambda x:x+1, start_point) + map(lambda x:x-1, end_point) ) block_matrix[postion_x][postion_y] = avhash( self.image_matrix[postion_x][postion_y] ) """ block_matrix[postion_x][postion_y] = make_regalur_image_histogram( self.image_matrix[postion_x][postion_y] ) """ same_block_dict = {} block_count = 1 for i, column in enumerate(block_matrix): for j, row in enumerate(column): if not row: continue finded = False for key, value in same_block_dict.iteritems(): if hamming(row, key) <= 8: #if hist_similar(row, key) >= 0.80: block_matrix[i][j] = value finded = True break if not finded: block_matrix[i][j] = block_count same_block_dict[row] = block_count block_count += 1 return block_matrix
def main(): print(ch.chebyshev(rd.random(10), rd.random(10))) print(hm.hamming(rd.random(10), rd.random(10))) print(mk.L_p(rd.random(10), rd.random(10), 0)) print(mk.L_p(rd.random(10), rd.random(10), 1)) print(mk.L_p(rd.random(10), rd.random(10), 2)) print(ma.mahalanobis(rd.random(10), rd.random(10))) print(ie.information_entropy((1, 2, 3, 4), (0.1, 0.2, 0.5, 0.7))) print(jc.jaccard_distance((1, 3, 4), (2, 4, 6))) print(jc.jaccard_similarity_coefficient((1, 3, 4), (1, 4, 6))) print(cs.cosine((1, 3, 4, 5), (2, 3, 4, 5)))
def mbxor_keysize(string): min_dist = 1000000 best_key_length = -1 for key_size in range(2, 41): normal_dist = 0.0 num_samples = int((len(string) - 1) / key_size / 2) for i in range(0, num_samples): n0bits = string[i * 2 * key_size:(i * 2 + 1) * key_size] n1bits = string[(i * 2 + 1) * key_size:(i * 2 + 2) * key_size] normal_dist += hamming(n0bits, n1bits) / float( key_size * num_samples) if normal_dist < min_dist: min_dist = normal_dist best_key_length = key_size return best_key_length
def neighbors(pattern, d): """ >>> neighbors('AA', 1) set(['AA', 'AC', 'AG', 'CA', 'AT', 'GA', 'TA']) """ if d == 0: return {pattern} if len(pattern) == 1: return set(_SYMBOLS) first, suffix = pattern[0], pattern[1:] neighborhood = set() for sn in neighbors(suffix, d): if hamming.hamming(suffix, sn) < d: for symbol in _SYMBOLS: neighborhood.add(symbol + sn) else: # hamming.hamming(suffix, sn) == d neighborhood.add(first + sn) return neighborhood
def _hammingcrack(enc): key_sizes = [] for key_size in range(1, MAX_KEY_SIZE): dist = 0.0 for i in range(HAMMING_DEPTH): block1 = enc[key_size*i:key_size*(i+1)] block2 = enc[key_size*(i+1):key_size*(i+2)] dist += hamming(block1, block2) dist /= key_size*HAMMING_DEPTH key_sizes.append([key_size, dist]) key_sizes.sort(key=lambda x: x[1]) keys = [] for index in range(KEY_DEPTH): key_size = key_sizes[index][0] key = crack(enc, key_size) plain = xor(enc, key) keys.append([key, fscore(plain)]) keys.sort(key=lambda x: x[1]) return keys[0][0]
def test_complete_hamming_distance_of_for_single_nucleotide_strand(self): self.assertEqual(1, hamming('A', 'G'))
def test_same(self): self.assertEqual(hamming("ATCG", "ATCG"), 0, "Hamming distance of two identical strings should be zero")
def test_complex(self): self.assertEqual(hamming("ATCGATCGGATC", "AGCGATCGGAGC"), 2, "A more complex example")
def test_hamming_small(self): self.assertEqual(1, hamming('GGACGA', 'GGTCGA'))
def test_hamming_different_length1(self): self.assertEqual(4, hamming('AAGCTAC', 'ACGTT'))
def test_no_difference_between_identical_strands(self): self.assertEqual(0, hamming('A', 'A'))
def rkxbreaker(cipherfile): with open(cipherfile, 'r+') as f: # print len(f.read()) b64ciptext = f.read().replace('\n', '') hextext = b642hex.b642hex(b64ciptext) # print len(b64ciptext) # print hextext, '\n' ndistances = [] for keysize in range(2, 41): # first keysizeworth: hextext[:keysize*2] # second keysizeworth: hextext[keysize*2:keysize*4] # third keysizeworth: hextext[keysize*4:keysize*6] # fourth keysizeworth: hextext[keysize*6:keysize*8] hdist12 = float( hamming.hamming(hextext[:keysize * 2], hextext[keysize * 2:keysize * 4])) / keysize hdist13 = float( hamming.hamming(hextext[:keysize * 2], hextext[keysize * 4:keysize * 6])) / keysize hdist14 = float( hamming.hamming(hextext[:keysize * 2], hextext[keysize * 6:keysize * 8])) / keysize hdist23 = float( hamming.hamming(hextext[keysize * 2:keysize * 4], hextext[keysize * 4:keysize * 6])) / keysize hdist24 = float( hamming.hamming(hextext[keysize * 2:keysize * 4], hextext[keysize * 6:keysize * 8])) / keysize hdist34 = float( hamming.hamming(hextext[keysize * 4:keysize * 6], hextext[keysize * 6:keysize * 8])) / keysize normdistance = (hdist12 + hdist13 + hdist14 + hdist23 + hdist24 + hdist34) / 6 # normdistance = float(hamming.hamming(hextext[:keysize*2],hextext[keysize*2:(keysize*4)]))/keysize # print hextext[:keysize*2],hextext[keysize*2:(keysize*4)] # print hextext[:keysize*2],hextext[keysize*2:keysize*4] # print hextext[keysize*4:keysize*6],hextext[keysize*6:keysize*8] # if keysize == 40: # print len(hextext[:keysize*2]),len(hextext[keysize*2:(keysize*4)]) # print hamming.hamming(hextext[:(keysize*2)],hextext[(keysize*2):(keysize*4)]) # print 'keysize: ',keysize,'distance: ', normdistance ndistances.append(normdistance) KEYSIZE = ndistances.index(min(ndistances)) + 2 # print 'KEYSIZE: ',KEYSIZE, '\n' # TRYING MANUALLY: # KEYSIZE = 29 # print KEYSIZE blockbuffer = '' blocks = [] transblocks = [] plaintblocks = [] for x in range(KEYSIZE): transblocks.append('') plaintblocks.append('') # print len(transblocks) for c in hextext: blockbuffer += c if len(blockbuffer) == KEYSIZE * 2: blocks.append(blockbuffer) blockbuffer = '' for i in blocks: for x in range(KEYSIZE): # print x*2,(x*2)+1 # print i[(x*2):((x*2)+2)] transblocks[x] += i[(x * 2):((x * 2) + 2)] for i in transblocks: # print transblocks.index(i) plaintblocks[transblocks.index(i)] = singlebxorcipher.sbxorcip(i) # key = '' plaintext = '' for i in range(len(plaintblocks[0])): for j in range(KEYSIZE): plaintext += (plaintblocks[j][i]) # print KEYSIZE # plainhex = plaintext.encode('hex') # print len(plainhex), len(hextext) # key = fixedxor.fixedxor(plainhex,hextext[:len(plainhex)])[:KEYSIZE*2].decode('hex') # print key # for i in range(KEYSIZE): # key += fixedxor.fixedxor(hextext[(2*i-1):(2*i)], str(ord(plaintext[i]))) # # print hextext[0],hextext[1], ord(plaintext[0]), ord(plaintext[1]) return plaintext
def test_multiple_dna_strand_differences(): assert hamming("CACGG", "GAGCC") == 4
def test_unequal_dna_strand_lengths(): with pytest.raises(ValueError): hamming("GGAG", "CATCG")
def test_identical_dna_strands(): assert hamming("GAGCC", "GAGCC") == 0
def test_one_dna_strand_difference(): assert hamming("GAGCG", "GAGCC") == 1
def test_count_long_dna_strands(): assert hamming("GAGCCTACTAACGGGAT", "CATCGTAATGACGGCCT") == 7
def test_hamming_different_length2(self): self.assertEqual(5, hamming("AAGCTAC", "ACGTTACGTC"))
from hamming import hamming from itertools import product pattern = "TGCA" k = 4 d = 3 nei=[] for p in product('ACGT', repeat = k): if hamming(pattern,p)<=d: nei.append(p) print len(nei)
def test_small_hamming_distance(self): self.assertEqual(1, hamming('AT', 'CT'))
def test_empty_dna_strands(): with pytest.raises(Exception): hamming("", "")
def test_large_hamming_distance(self): self.assertEqual(4, hamming('GATACA', 'GCATAA'))
def test_null_right(): #*****************************************************************************80 # ## TEST_NULL_RIGHT tests right null vectors. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 12 March 2015 # # Author: # # John Burkardt # from a123 import a123 from a123 import a123_null_right from archimedes import archimedes from archimedes import archimedes_null_right from cheby_diff1 import cheby_diff1 from cheby_diff1 import cheby_diff1_null_right from creation import creation from creation import creation_null_right from dif1 import dif1 from dif1 import dif1_null_right from dif1cyclic import dif1cyclic from dif1cyclic import dif1cyclic_null_right from dif2cyclic import dif2cyclic from dif2cyclic import dif2cyclic_null_right from fibonacci1 import fibonacci1 from fibonacci1 import fibonacci1_null_right from hamming import hamming from hamming import hamming_null_right from line_adj import line_adj from line_adj import line_adj_null_right from moler2 import moler2 from moler2 import moler2_null_right from neumann import neumann from neumann import neumann_null_right from one import one from one import one_null_right from r8_uniform_ab import r8_uniform_ab from r8mat_is_null_right import r8mat_is_null_right from r8mat_norm_fro import r8mat_norm_fro from r8vec_norm_l2 import r8vec_norm_l2 from ring_adj import ring_adj from ring_adj import ring_adj_null_right from rosser1 import rosser1 from rosser1 import rosser1_null_right from zero import zero from zero import zero_null_right print '' print 'TEST_NULL_RIGHT' print ' A = a test matrix of order M by N' print ' x = an N vector, candidate for a right null vector.' print '' print ' ||A|| = Frobenius norm of A.' print ' ||x|| = L2 norm of x.' print ' ||A*x||/||x|| = L2 norm of A*x over L2 norm of x.' print '' print ' Title M N ', print '||A|| ||x|| ||A*x||/||x||' print '' # # A123 # title = 'A123' m = 3 n = 3 a = a123() x = a123_null_right() error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # ARCHIMEDES # title = 'ARCHIMEDES' m = 7 n = 8 a = archimedes() x = archimedes_null_right() error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # CHEBY_DIFF1 # title = 'CHEBY_DIFF1' m = 5 n = m a = cheby_diff1(n) x = cheby_diff1_null_right(m, n) error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # CREATION # title = 'CREATION' m = 5 n = m a = creation(m, n) x = creation_null_right(m, n) error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # DIF1 # Only has null vectors for N odd. # title = 'DIF1' m = 5 n = m a = dif1(m, n) x = dif1_null_right(m, n) error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # DIF1CYCLIC # title = 'DIF1CYCLIC' m = 5 n = m a = dif1cyclic(n) x = dif1cyclic_null_right(m, n) error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # DIF2CYCLIC # title = 'DIF2CYCLIC' m = 5 n = m a = dif2cyclic(n) x = dif2cyclic_null_right(m, n) error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # FIBONACCI1 # title = 'FIBONACCI1' m = 5 n = m r8_lo = -5.0 r8_hi = +5.0 seed = 123456789 f1, seed = r8_uniform_ab(r8_lo, r8_hi, seed) f2, seed = r8_uniform_ab(r8_lo, r8_hi, seed) a = fibonacci1(n, f1, f2) x = fibonacci1_null_right(m, n, f1, f2) error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # HAMMING # title = 'HAMMING' m = 5 n = (2**m) - 1 a = hamming(m, n) x = hamming_null_right(m, n) error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # LINE_ADJ # title = 'LINE_ADJ' m = 7 n = m a = line_adj(n) x = line_adj_null_right(m, n) error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # MOLER2 # title = 'MOLER2' m = 5 n = 5 a = moler2() x = moler2_null_right() error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # NEUMANN # title = 'NEUMANN' row_num = 5 col_num = 5 m = row_num * col_num n = row_num * col_num a = neumann(row_num, col_num) x = neumann_null_right(row_num, col_num) error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # ONE # title = 'ONE' m = 5 n = 5 a = one(m, n) x = one_null_right(m, n) error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # RING_ADJ # N must be a multiple of 4 for there to be a null vector. # title = 'RING_ADJ' m = 12 n = 12 a = ring_adj(m, n) x = ring_adj_null_right(m, n) error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # ROSSER1 # title = 'ROSSER1' m = 8 n = 8 a = rosser1() x = rosser1_null_right() error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # ZERO # title = 'ZERO' m = 5 n = 5 a = zero(m, n) x = zero_null_right(m, n) error_l2 = r8mat_is_null_right(m, n, a, x) norm_a_frobenius = r8mat_norm_fro(m, n, a) norm_x_l2 = r8vec_norm_l2(n, x) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # Terminate. # print '' print 'TEST_NULL_RIGHT:' print ' Normal end of execution.' return
s = sortBySuitability(t) assert (s[-1]['xor'] == b'Now that the party is jumping\n') #Exercise 5 ee = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal" assert ( hexlify(xor(bytes(ee, 'utf-8'), bytes("ICE", 'utf-8'))) == b'0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f' ) #Exercise 6 q = 'this is a test' z = 'wokka wokka!!!' assert (hamming(q, z) == 37) assert (hamming(bytes(q, 'utf-8'), bytes(z, 'utf-8')) == 37) f = open('6.txt', 'r') x = f.read() base = b64decode(x) bb = hexlify(base) key = b'' for b in block(bb, findLowestHammingLength(base)): sort = sortBySuitability(trials(unhexlify(b))) key += sort[-1]['key'] lyrics = b"I'm back and I'm ringin' the bell \nA rockin' on the mike while the fly girls yell \nIn ecstasy in the back of me \nWell that's my DJ Deshay cuttin' all them Z's \nHittin' hard and the girlies goin' crazy \nVanilla's on the mike, man I'm not lazy. \n\nI'm lettin' my drug kick in \nIt controls my mouth and I begin \nTo just let it flow, let my concepts go \nMy posse's to the side yellin', Go Vanilla Go! \n\nSmooth 'cause that's the way I will be \nAnd if you don't give a damn, then \nWhy you starin' at me \nSo get off 'cause I control the stage \nThere's no dissin' allowed \nI'm in my own phase \nThe girlies sa y they love me and that is ok \nAnd I can dance better than any kid n' play \n\nStage 2 -- Yea the one ya' wanna listen to \nIt's off my head so let the beat play through \nSo I can funk it up and make it sound good \n1-2-3 Yo -- Knock on some wood \nFor good luck, I like my rhymes atrocious \nSupercalafragilisticexpialidocious \nI'm an effect and that you can bet \nI can take a fly girl and make her wet. \n\nI'm like Samson -- Samson to Delilah \nThere's no denyin', You can try to hang \nBut you'll keep tryin' to get my style \nOver and over, practice makes perfect \nBut not if you're a loafer. \n\nYou'll get nowhere, no place, no time, no girls \nSoon -- Oh my God, homebody, you probably eat \nSpaghetti with a spoon! Come on and say it! \n\nVIP. Vanilla Ice yep, yep, I'm comin' hard like a rhino \nIntoxicating so you stagger like a wino \nSo punks stop trying and girl stop cryin' \nVanilla Ice is sellin' and you people are buyin' \n'Cause why the freaks are jockin' like Crazy Glue \nMovin' and groovin' trying to sing along \nAll through the ghetto groovin' this here song \nNow you're amazed by the VIP posse. \n\nSteppin' so hard like a German Nazi \nStartled by the bases hittin' ground \nThere's no trippin' on mine, I'm just gettin' down \nSparkamatic, I'm hangin' tight like a fanatic \nYou trapped me once and I thought that \nYou might have it \nSo step down and lend me your ear \n'89 in my time! You, '90 is my year. \n\nYou're weakenin' fast, YO! and I can tell it \nYour body's gettin' hot, so, so I can smell it \nSo don't be mad and don't be sad \n'Cause the lyrics belong to ICE, You can call me Dad \nYou're pitchin' a fit, so step back and endure \nLet the witch doctor, Ice, do the dance to cure \nSo come up close and don't be square \nYou wanna battle me -- Anytime, anywhere \n\nYou thought that I was weak, Boy, you're dead wrong \nSo come on, everybody and sing this song \n\nSay -- Play that funky music Say, go white boy, go white boy go \nplay that funky music Go white boy, go white boy, go \nLay down and boogie and play that funky music till you die. \n\nPlay that funky music Come on, Come on, let me hear \nPlay that funky music white boy you say it, say it \nPlay that funky music A little louder now \nPlay that funky music, white boy Come on, Come on, Come on \nPlay that funky music \n" assert (xor(base, key) == lyrics) #Exercise 7 seven = open('7.txt', 'r')
def test_hamming_different_length1(self): self.assertEqual(4, hamming("AAGCTAC", "ACGTT"))
def test_hamming_large(self): self.assertEqual(4, hamming('GGATCG', 'CCTGCG'))
#Exercise 2 aa = '1c0111001f010100061a024b53535009181c' bb = '686974207468652062756c6c277320657965' assert (hexlify(xor(aa, bb)) == bytes('746865206b696420646f6e277420706c6179', 'utf-8')) #Exercise 3 dd = '1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736' assert (dec(dd)[0] == b"Cooking MC's like a pound of bacon") #Exercise 4 f = open('4.txt', 'r') for line in f.read().splitlines(): x = dec(line) if (x != []): four = x[0] assert (four == b'Now that the party is jumping\n') #Exercise 5 ee = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal" assert ( hexlify(xor(hexFromString(ee), hexFromString("ICE"))) == b'0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f' ) #Exercise 6 q = 'this is a test' z = 'wokka wokka!!!' assert (hamming(q, z) == 37)
def test_hamming_very_long(self): self.assertEqual(9, hamming('GGACGGATTCTG', 'AGGACGGATTCT'))
f = open('decryptThis', 'r') encoded = f.read() encoded = ''.join(encoded.split()) encoded = encoded.decode('base64').encode('hex') possible = [] # print(len(encoded)) for size in range(2, 41): keysize = 2 * size chunk1 = encoded[0:keysize] chunk2 = encoded[keysize:2 * keysize] chunk3 = encoded[2 * keysize:3 * keysize] chunk4 = encoded[3 * keysize:4 * keysize] distance1 = hamming(chunk1, chunk2) distance2 = hamming(chunk3, chunk4) normalised = float(distance1 + distance2) / float(keysize) possible.append((size, normalised)) possible = sorted(possible, key=lambda x: x[1]) print(possible) ############################################################### # KEYSIZE is 29 # KEY is `Terminator X: Bring the noise` ############################################################### for size, _ in possible[:10]: KEYSIZE = size
def test_hamming_different_length2(self): self.assertEqual(5, hamming('AAGCTAC', 'ACGTTACGTC'))
def test_hamming_very_long(self): self.assertEqual(9, hamming("GGACGGATTCTG", "AGGACGGATTCT"))
def test_different(self): self.assertEqual(hamming("GCTA", "ATCG"), 4, "Hamming distance of two completely different strings")
def test_hamming_empty(self): self.assertEqual(0, hamming('',''))
def test_empty(self): self.assertEqual(hamming("", ""), 0, "Hamming distance of two empty strings should be zero")
def test_hamming_onenucleotide_different(self): self.assertEqual(1, hamming('A','G'))
def test_complete_hamming_distance_of_for_small_strand(self): self.assertEqual(2, hamming('AG', 'CT'))
def test_hamming_short2(self): self.assertEqual(2, hamming('AG','CT'))
def test_small_hamming_distance_in_longer_strand(self): self.assertEqual(1, hamming('GGACG', 'GGTCG'))
def test_hamming_small(self): self.assertEqual(1, hamming('GGACGA','GGTCGA'))
def test_hamming_distance_in_very_long_strand(self): self.assertEqual(9, hamming('GGACGGATTCTG', 'AGGACGGATTCT'))
def test_hamming_different_length1(self): self.assertEqual(4, hamming('AAGCTAC','ACGTT'))
def test_hamming_small(self): self.assertEqual(1, hamming("GGACGA", "GGTCGA"))
def test_hamming_large(self): self.assertEqual(4, hamming("GGATCG", "CCTGCG"))
def test_hamming_empty(self): self.assertEqual(0, hamming('', ''))
def test_null_right ( ): #*****************************************************************************80 # ## TEST_NULL_RIGHT tests right null vectors. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 12 March 2015 # # Author: # # John Burkardt # from a123 import a123 from a123 import a123_null_right from archimedes import archimedes from archimedes import archimedes_null_right from cheby_diff1 import cheby_diff1 from cheby_diff1 import cheby_diff1_null_right from creation import creation from creation import creation_null_right from dif1 import dif1 from dif1 import dif1_null_right from dif1cyclic import dif1cyclic from dif1cyclic import dif1cyclic_null_right from dif2cyclic import dif2cyclic from dif2cyclic import dif2cyclic_null_right from fibonacci1 import fibonacci1 from fibonacci1 import fibonacci1_null_right from hamming import hamming from hamming import hamming_null_right from line_adj import line_adj from line_adj import line_adj_null_right from moler2 import moler2 from moler2 import moler2_null_right from neumann import neumann from neumann import neumann_null_right from one import one from one import one_null_right from r8_uniform_ab import r8_uniform_ab from r8mat_is_null_right import r8mat_is_null_right from r8mat_norm_fro import r8mat_norm_fro from r8vec_norm_l2 import r8vec_norm_l2 from ring_adj import ring_adj from ring_adj import ring_adj_null_right from rosser1 import rosser1 from rosser1 import rosser1_null_right from zero import zero from zero import zero_null_right print '' print 'TEST_NULL_RIGHT' print ' A = a test matrix of order M by N' print ' x = an N vector, candidate for a right null vector.' print '' print ' ||A|| = Frobenius norm of A.' print ' ||x|| = L2 norm of x.' print ' ||A*x||/||x|| = L2 norm of A*x over L2 norm of x.' print '' print ' Title M N ', print '||A|| ||x|| ||A*x||/||x||' print '' # # A123 # title = 'A123' m = 3 n = 3 a = a123 ( ) x = a123_null_right ( ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # ARCHIMEDES # title = 'ARCHIMEDES' m = 7 n = 8 a = archimedes ( ) x = archimedes_null_right ( ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # CHEBY_DIFF1 # title = 'CHEBY_DIFF1' m = 5 n = m a = cheby_diff1 ( n ) x = cheby_diff1_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # CREATION # title = 'CREATION' m = 5 n = m a = creation ( m, n ) x = creation_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # DIF1 # Only has null vectors for N odd. # title = 'DIF1' m = 5 n = m a = dif1 ( m, n ) x = dif1_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # DIF1CYCLIC # title = 'DIF1CYCLIC' m = 5 n = m a = dif1cyclic ( n ) x = dif1cyclic_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # DIF2CYCLIC # title = 'DIF2CYCLIC' m = 5 n = m a = dif2cyclic ( n ) x = dif2cyclic_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # FIBONACCI1 # title = 'FIBONACCI1' m = 5 n = m r8_lo = -5.0 r8_hi = +5.0 seed = 123456789 f1, seed = r8_uniform_ab ( r8_lo, r8_hi, seed ) f2, seed = r8_uniform_ab ( r8_lo, r8_hi, seed ) a = fibonacci1 ( n, f1, f2 ) x = fibonacci1_null_right ( m, n, f1, f2 ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # HAMMING # title = 'HAMMING' m = 5 n = ( 2 ** m ) - 1 a = hamming ( m, n ) x = hamming_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # LINE_ADJ # title = 'LINE_ADJ' m = 7 n = m a = line_adj ( n ) x = line_adj_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # MOLER2 # title = 'MOLER2' m = 5 n = 5 a = moler2 ( ) x = moler2_null_right ( ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # NEUMANN # title = 'NEUMANN' row_num = 5 col_num = 5 m = row_num * col_num n = row_num * col_num a = neumann ( row_num, col_num ) x = neumann_null_right ( row_num, col_num ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # ONE # title = 'ONE' m = 5 n = 5 a = one ( m, n ) x = one_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # RING_ADJ # N must be a multiple of 4 for there to be a null vector. # title = 'RING_ADJ' m = 12 n = 12 a = ring_adj ( m, n ) x = ring_adj_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # ROSSER1 # title = 'ROSSER1' m = 8 n = 8 a = rosser1 ( ) x = rosser1_null_right ( ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # ZERO # title = 'ZERO' m = 5 n = 5 a = zero ( m, n ) x = zero_null_right ( m, n ) error_l2 = r8mat_is_null_right ( m, n, a, x ) norm_a_frobenius = r8mat_norm_fro ( m, n, a ) norm_x_l2 = r8vec_norm_l2 ( n, x ) print ' %-20s %4d %4d %14g %14g %10.2g' \ % ( title, m, n, norm_a_frobenius, norm_x_l2, error_l2 ) # # Terminate. # print '' print 'TEST_NULL_RIGHT:' print ' Normal end of execution.' return
def test_hamming_onenucleotide_same(self): self.assertEqual(0, hamming('A','A'))
def test_hamming_onenucleotide_same(self): self.assertEqual(0, hamming('A', 'A'))
def test_hamming_short1(self): self.assertEqual(1, hamming('AT','CT'))
def test_hamming_onenucleotide_different(self): self.assertEqual(1, hamming('A', 'G'))
def test_hamming_large(self): self.assertEqual(4, hamming('GGATCG','CCTGCG'))
def test_hamming_short1(self): self.assertEqual(1, hamming('AT', 'CT'))
def test_hamming_very_long(self): self.assertEqual(9, hamming('GGACGGATTCTG','AGGACGGATTCT'))
def test_hamming_short2(self): self.assertEqual(2, hamming('AG', 'CT'))
def test_hamming_different_length2(self): self.assertEqual(5, hamming('AAGCTAC','ACGTTACGTC'))
def test_hamming_short2(self): self.assertEqual(2, hamming("AG", "CT"))