Caesar Cipher

  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    def caesar_cipher(s, k):
        # Time complexity: O(n)
        # Space complexity (ignoring input): O(n)
        new_string = ""
        for letter in s:
            if (letter.lower() <= "z") and (letter.lower() >= "a"):
                if letter == letter.lower():
                    sum_a = ord("a")
                else:
                    sum_a = ord("A")
                new_string += chr(
                    ((ord(letter) - sum_a + k) % (ord("z") - ord("a") + 1)) + sum_a
                )
            else:
                new_string += letter
    
        return new_string