
It's nicely structured, readable and documented.
CAESAR SHIFT CIPHER CODE
Generally speaking your code looks quite good from my point of view. Setup="from _main_ import caesar_cipher cipher = caesar_cipher(2)", The real speed will come from not creating a new translation table on each call. Return str.maketrans(ascii_lowercase + ascii_uppercase,Īscii_lowercase + ascii_lowercase +Īscii_uppercase + ascii_uppercase) Using these functions, you can easily construct a Caesar cipher encoder-decoder: from string import ascii_lowercase, ascii_uppercase

Must be a string, whose characters will be mapped to None in the result. In the resulting dictionary, each character in x will be mapped to theĬharacter at the same position in y. If there are two arguments, they must be strings of equal length, and Ordinals (integers) or characters to Unicode ordinals, strings or None.Ĭharacter keys will be then converted to ordinals. If there is only one argument, it must be a dictionary mapping Unicode Return a translation table usable for str.translate(). It has a str.maketrans function for creation of the translation table: > help(str.maketrans) If this operation raises LookupError, the character is The table must implement lookup/indexing via _getitem_, for instance aĭictionary or list. Translation table, which must be a mapping of Unicode ordinals to Replace each character in the string using the given translation table. As a built-in, it is blazingly fast compared to processing each letter individually: > help(str.translate) Python comes with a str.translate, function which will do a letter-for-letter substitution in a string, which is exactly what you want to do. Let Python do the work for you, with ALPHABET_LENGTH = len(ascii_lowercase)Īvoid String concatenation use built-in functions You don't need to hard-code ALPHABET_LENGTH = 26 in your program. Setup="from _main_ import encode_string, ALPHABET_LENGTH, LOWERCASE, UPPERCASE", # value of n used to index constants, equivalent to -n steps in backwards direction # Incase of very large n, find lowest equivalent offset Positive for a forward shift, negative for a backwards shift :param n: the number of places in the alphabet to shift the letter. :param s: the str on which to perform a Cipher shift """Perform a Caesar Cipher shift on s leaving punctuation, spaces intact UPPERCASE = ascii_uppercase + ascii_uppercaseĭef encode_string(s: str, n: int) -> str:

LOWERCASE = ascii_lowercase + ascii_lowercase

The string is "Caesar Cipher" and num is 2 the output should beįrom string import ascii_lowercase, ascii_uppercaseįormat="%(asctime)s - %(levelname)s - %(message)s") Punctuation, spaces, and capitalization should remain intact. String N places down in the alphabet (in this case N will be num). A Caesar Cipher works by shifting each letter in the Str parameter and perform a Caesar Cipher shift on it using the num parameterĪs the shifting number. Using the Python language, have the function CaesarCipher(str,num) take the I've tried it fast in any way I know how to, but, what might be a more efficient and/or pythonic solution? I'd like feedback on my solution to the outlined programming challenge (medium level).
