#from random import randint code = {'A': 0, 'B': 1,'C': 2,'D': 3, 'E': 4,'F': 5,'G': 6,'H': 7, 'I': 8,'J': 9,'K': 10,'L': 11, 'M': 12,'N': 13,'O': 14,'P': 15, 'Q': 16,'R': 17,'S': 18,'T': 19, 'U': 20,'V': 21,'W': 22, 'X': 23, 'Y': 24,'Z': 25} reverse_code = dict((v,k) for k, v in code.iteritems()) #specify reverse- #associated dictionary message = "WEAREDISCOVEREDFLEEATONCE" key_word = "ZEBRAS" key = [] def fun_matrix(key_word, message): k = key_word m = message row_length = len(key_word) column_length = len(message) / len(key_word) rows = [] matrix = [] b = 0 t = row_length c = 0 while c <= column_length: for a in message[b:t]: rows.append(a) matrix.append(rows) rows = [] b += row_length t = b + row_length c += 1 print matrix while len(matrix[column_length]) < row_length: null = "A" #randint(0, 25) matrix[column_length].append(null) print matrix return matrix def fun_column(matrix, column_length, num): column = [] a = 0 while a <= column_length: column.append(matrix[a][num]) a += 1 return column def fun_ranked_list(x): """ returns the order of each element in x as a list. """ L = len(x) rangeL = range(L) z = izip(x, rangeL) z = izip(z, rangeL) #avoid problems with duplicates. D = sorted(z) return [d[1] for d in D] def encoder(message, key_word): matrix = fun_matrix(key_word, message) column_length = len(message) / len(key_word) #for a, b in zip(ranked_list(key_word) ranked_list = fun_ranked_list(key_word) #column = fun_column(matrix, column_length, num) x = 0 print ranked_list while x < len(ranked_list): if x == min(ranked_list): print fun_column(matrix, column_length, x) ranked_list.remove(x) print ranked_list x += 1 print x if x == len(ranked_list): x = 0 #if len(ranked_list) == 0: #def encode_or_decode(message, key_word): #encode_decode = raw_input("do you want to encode or decode? ") #.ascii_lowercase #message = raw_input("Enter message: ")#.ascii_uppercase #key_word = raw_input("Enter key: ")#.ascii_uppercase #if encode_decode == "encode": #encoder(message, key_word) #if encode_decode == "decode": #decoder(message, key_word) encoder(message, key_word)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question