简体   繁体   中英

encrypt/decrypt with rot13 using a lambda?

I need to crypt/decrypt using a lambda and I got some problem with it. My function create_rot13() must not receive an argument, only my lambda should.

This my current code so far, not using any lambda as I couldn't find one (after several days of looking around). How to put so lines of code into it.

def create_rot13(message):
    crypted = ""
    letters = "abcdefghijklmnopqrstuvwxyz"

    for car in message:
        if car in letters:
            num = letters.find(car)
            num = num + 13
            if num >= len(letters):
                num = num - len(letters)
            crypted = crypted + letters[num]
    else:
        crypted = crypted + car

    crypted = crypted[:-1]
    return crypted

print(create_rot13("jbeyq"))
print(create_rot13("world"))

Anyone have tips or something to help me out to find the solution to my problem?

It should look sort of like that, except this 1 change only a number:

def create_rot13():
 my_fonction = lambda x : x + 13
 return my_fonction

coding = create_rot13()
print(coding(4))

If you want to do it with a lambda, then you need to capture the letters variable with the lambda and also use map . You want something like this:

def encode_rot13(message):
    offset = ord('a')
    result = map(lambda c: chr((ord(c) + 13 - offset) % 26 + offset), message)
    return "".join(result)

I use chr and ord so we don't need to keep around a string with all letters. The idea here is that we are getting an integer that represents the codepoint for any given character, so we can do some math instead of using find .

Here's a simple one which (ab)uses the codecs module (and a lambda). Note that you need the codecs module here instead of just a normal call to .encode('rot13') as you're doing a text -> text encoding.

import codecs
rot13 = lambda s: codecs.encode(s, 'rot13')

Here's some sample usage:

>>> rot13('foo')
'sbb'

Here's my solution:

rot13 = lambda m: ''.join(chr(ord(c)+13) if 'a' <= c.lower() < 'n' else chr(ord(c)-13) if 'm' < c.lower() <= 'z' else c for c in m)

I just used it on this , which is precisely rot13-encoded:

import this
# output
print(rot13(this.s))
# same output

I'm making the lambda take a string and return a string. I'm doing this on a joined generator, and only rotating non-accented letters, regardless of case. My version accepts a rotation length and filters it for the [0-26] interval:

rot = lambda n, m: ''.join(chr(ord(c)+n) if 'a' <= c.lower() <= chr(ord('z')-n) else chr(ord(c)+n-26) if chr(ord('z')-n) < c.lower() <= 'z' else c for c in m) if 0 < n < 27 else ''

In order to use it, you need to pass the message and the number of letters to rotate. So, rot13 would be the same as a partial application on rot , such that rot13(message) == rot(13, message) . You could then define rot13 like this:

rot13 = lambda m: rot(13, m)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM