random - Enciphering and Deciphering by Shuffling in Python -
i writing program enciphers (and decipher) given string.
the encipher function takes 2 arguments: string , seed value.
here have far:
def random_encipher(string,seed): random.seed(seed) alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] #shuffle alphabet random.shuffle(alphabet) #assign index each letter in alphabet letter in alphabet: letter = ord(letter)-97
to sum up, i'm shuffling alphabet , assigning each letter number value ("a" = 0, "b" = 1, . . .)
here's need with:
i need string[0] printed alphabet[0] (which shuffled alphabet, therefore current seed value, alphabet[0] = "e").
but each letter of string, not 0 index.
maybe that?
>>> import random >>> def random_encipher(string,seed): random.seed(seed) alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] #shuffle alphabet random.shuffle(alphabet) ciphers = [] #assign index each letter in alphabet letter in string: index = ord(letter)-97 cipher = alphabet[index] ciphers.append(cipher) return "".join(ciphers) >>> random_encipher("foobar", 3) 'fwwqgc'
the point in using list, strings immutable, appending string requires string copied costly. appending list , merging elements @ end better choice (or use stringio).
Comments
Post a Comment