Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shift cipher in Python: error using ord [duplicate]

I want to replace each character of a string by a different one, shifted over in the alphabet. I'm shifting by 2 in the example below, so a -> c, b -> d, etc.

I'm trying to use a regular expression and the sub function to accomplish this, but I'm getting an error.

This is the code that I have:

p = re.compile(r'(\w)')
test = p.sub(chr(ord('\\1') + 2), text)
print test

where the variable text is an input string.

And I'm getting this error:

TypeError: ord() expected a character, but string of length 2 found

I think the problem is that I the ord function is being called on the literal string "\1" and not on the \w character matched by the regular expression. What is the right way to do this?

like image 555
Joe Avatar asked Aug 07 '26 15:08

Joe


2 Answers

It won't work like this. Python first runs chr(ord('\\') + 2 and then passes that result to p.sub.

You need to put it in a separate function or use an anonymous function (lambda):

p = re.compile(r'(\w)')
test = p.sub(lambda m: chr(ord(m.group(1)) + 2), text)
print test

Or better yet use maketrans instead of regular expressions:

import string

shift = 2

t = string.maketrans(string.ascii_lowercase, string.ascii_lowercase[shift:] +
                                             string.ascii_lowercase[:shift])
string.translate(text, t)
like image 190
Rob Wouters Avatar answered Aug 10 '26 11:08

Rob Wouters


Full version

def shouldShift(char):
    return char in string.lowercase

def caesarShift(string, n):
    def letterToNum(char):
        return ord(char)-ord('a')
    def numToLetter(num):
        return chr(num+ord('a'))

    def shiftByN(char):
        return numToLetter((letterToNum(char)+n) % 26)

    return ''.join((shiftByN(c) if shouldShift(c) else c) for c in string.lower())

One-liner

If you really want a one-liner, it would be this, but I felt it was uglier:

''.join(chr((ord(c)-ord('a')+n)%26 + ord('a')) for c in string)

Demo

>>> caesarShift(string.lowercase, 3)
'defghijklmnopqrstuvwxyzabc'
like image 23
ninjagecko Avatar answered Aug 10 '26 12:08

ninjagecko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!