954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

One-line Caesar Cipher help

I'm attempting to create a one line Caesar cipher decryption program in python and i'm afraid i've come to the end of my knowledge and well past it.

this is the closest working code i've got:

def test3(): 
    print("The decrypted text is: "+"".join(map(lambda x: chr(ord(x)+y),x)))


as you can see it requires me to define x and y (the input text and the shift amount respectively) before hand. this wont do, next i tried this (and several variations of)

def test4(): 
    print("The decrypted text is: "+"".join(map(lambda x=input("text: "),y=eval(input("shift: ")): chr(ord(x)+y),x)))


but x is undefined globally

the function should run as follows:

>>>text: |nqq
>>>shift: -5
The decrypted text is: will


any help would be greatly appreciated
ps. using python 3.1.3

animanga
Newbie Poster
8 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

Why do not use the function you defined by having parameters and return value instead of print? int is better (safer) than eval on user input.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

i've updated the code to:

def test8(): #input works
    z=lambda x=input("Enter the text to be decrypted: "),y=int(input("Enter the shift amount: ")): print("The decrypted text is: "+"".join(list(map(lambda x: chr(ord(x)+y),x))))
    return z()


is this what you meant?
also is there anyway i can do this without the return z() line?
i tried just replacing z=lambda... with return lambda... but i just get something along these lines:

<function <lambda> at 0x0000000003A698C8>
animanga
Newbie Poster
8 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

This is not Lisp however much it is my first love in programming languages, study about defining functions (or procedures, they are same in Python), also study about generators and use join as you have already done to get result.

Input part cannot be counted as part of one liner function, but is part of calling the function like:

encrypted = caesar(original=input("Enter the text to be decrypted: "), shift=int(input("Enter the shift amount: ")))

Notice the descriptive parameter names.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

Hi,
You can use () to make it work (if you really want a one liner)...

def l():
    return (lambda ...)()
def l():
    return (lambda x=input("Enter the text to be decrypted: "),y=int(input("Enter the shift amount: ")): print("The decrypted text is: "+"".join(list(map(lambda x: chr(ord(x)+y),x)))))()
jice
Posting Whiz in Training
225 posts since Oct 2007
Reputation Points: 64
Solved Threads: 57
 

thank you both for the help, jice, it worked perfectly thanks!
and tonyjv i decided take your advice and prettied up my variable names a bit

final code:

def decoder(): #SUCCESS
    return (lambda encryptedText=input("Enter the text to be decrypted: "),shift=int(input("Enter the shift amount: ")): print("The decrypted text is: "+"".join(list(map(lambda encryptedText: chr(ord(encryptedText)+shift),encryptedText)))))()
animanga
Newbie Poster
8 posts since Jul 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: