Caesar Cipher

  • + 0 comments
    def caesarCipher(s, k):
        res=""
        for i in range(len(s)):
            small, ch = check('a','z', s, i, k)
            if not small:
                big, ch = check('A', 'Z', s, i, k)
            res+=ch
        return res
    
    def check(start, end, s, i,  k):
        n = ord(end)-ord(start)+1
        ch = s[i]
        if ord(start) <= ord(ch) <= ord(end):
            i = ord(ch) - ord(start)
            new_i = (i + k) % n
            return True, chr(ord(start) + new_i)
        return False, ch