def encode(s, n): #This function will encode plain text to the cipher I made s+=" "*(len(s)%n) #Makes sure that len(s) is divisible exactly by n split_s=[] encoded_s = "" #Setting up variables for x in range(n): #This will seperate s into n equal strings split_s.append(s[x*(int(len(s)/n)):(x+1)*(int(len(s)/n))]) #Appends each equal string to split_s zip_s=list(zip(*split_s)) #Matches all the first letters of all the strings to each other; all the second to each other; etc. for x in zip_s: for y in x: encoded_s+=y #Takes each individual letter and puts into the result, encoded_s print (encoded_s) return (encoded_s) #Returning and printing def decode(s,n): #This will turn the coded string into plain text decoded_s="" #Setting up variables for x in range(n): #Will extract each of the split strings decoded_s+=str(s[x::int(n)]) #By doing list slicing with the third number as n, it splits the coded letters into the original strings that were split from s print (decoded_s) return (decoded_s) #Returning and printing decode(encode("I wrote this cool program that can cipher and decipher text. Try it!",2),2) #Sample usage
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