Caesar Cipher

  • + 0 comments
    def caesarCipher(s, k):
        # Write your code here
        alphabet = 'abcdefghijklmnopqrstuvwxyz'
        k = k % 26
        alphabet_rot = alphabet[k:] + alphabet[:k]
        result = []
        for i in range(len(s)):
            for idx, char in enumerate(alphabet):
                if s[i] == char:
                    result.append(alphabet_rot[idx])
                elif s[i] == char.upper():
                    result.append(alphabet_rot[idx].upper())
            if s[i].isalpha() == False:
                result.insert(i, s[i])
        return ''.join(result)