I wrote the below code and tested some example files on it. I didn't find any errors, but I still want to be thorough about this.
Also, any suggestions in improving the code?
import string
""" this implements an encryption standard over the message text
it replaces the odd place alphabets by their ordinal number plus place number increased.
it replaces the even place alphabets by their ordinal number minus place number decreased.
for example "hello" becomes "icoht" and "abc" becomes "bzf".
"""
input_filename = raw_input("Enter the input file name: ")
output_filename = raw_input("Enter the output dumping file name: ")
mode = raw_input("Do you want to encrypt or decrypt (e/d)?: ")
with open(input_filename,'r') as inputfile:
with open(output_filename,'w') as outputfile:
inp = inputfile.read()
encode = lambda s: "".join(
(chr(ord('a')+((ord(c)-(ord('a')+i+1))%26))) if ((c.isalpha()) and ((i+1)% 2 ==0))
else
string.ascii_lowercase[(string.ascii_lowercase.rfind(c.lower())+i+1)%26] if ((c.isalpha()) and ((i+1)% 2 ==1))
else
c
for i,c in enumerate(s.lower())
)
decode = lambda s: "".join(
string.ascii_lowercase[(string.ascii_lowercase.rfind(c.lower())+i+1)%26] if ((c.isalpha()) and ((i+1)% 2 ==0))
else
(chr(ord('a')+((ord(c)-(ord('a')+i+1))%26))) if ((c.isalpha()) and ((i+1)% 2 ==1))
else
c
for i,c in enumerate(s.lower())
)
if(mode.lower() == 'e'):
outputfile.write(encode(inp))
else :#(mode.lower() =='d'):
outputfile.write(decode(inp))
s = {'e':'Encoding','d':'Decoding'}
print s.get(mode)+" Sucessfull"
((i+1)% 2) ==0)
is more traditionally written as(i % 2 == 1)
. Conversely,((i+1)% 2) == 1)
is the same as(i % 2 == 0)
. \$\endgroup\$