How to perform a Caesar cipher with multiple shifts

For example, if I applied 4 shifts to the "breakzone", what it would look like:

"b" will be shifted by 2 characters "r" will be shifted by 3 characters "e" will be shifted by 4 characters "a" will be shifted by 5 characters and then we will return to the initial shift amount at 2: "k" will be shifted by 2 symbol etc ...

here is what I have so far:

statement = input("Enter string: ")
multiple_shifts = input(int("Enter shift amounts: "))
def caesar(word,multiple_shifts):
    for ch in statement:
        if ch = statement[0]:

      

I know this isn't much, but I am definitely lost and any help is appreciated. Without asking you to "write your own code", just point me in the right direction, so I would really appreciate any knowledge of this topic.

+3


source to share


1 answer


Here's a very rough implementation:

import sys

def caesar(word, shifts):
    word_list = list(word.lower())
    result = []
    s = 0
    for c in word_list:
        next_ord = ord(c) + s + 2
        if next_ord > 122:
            next_ord = 97

        result.append(chr(next_ord))
        s = (s + 1) % shifts
    return "".join(result)

if __name__ == "__main__":
    print(caesar("breakzone", 4))

      



If you're wondering what with 122

and 97

, these are the lowercase Unicode values z

and a

. You can easily modify the above to support full Unicode (like being able to encode H3ll0

, for example). A Unicode diagram can be found here that might help you.

+2


source







All Articles