DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Joseph Morante
Joseph Morante

Posted on

Day 3 of #100daysofcode

Third times the charm, day 3 of #100daysofcode. For day 3 of coding I got to work on an interesting little code project called caser cipher. My initial experience with this problem was interesting since I was on good roll breezing through these problems until I had to do this. It was mixture of self doubt and excitement, on the one hand I'm struggling with a simple problem but at the same time it means I have to push myself to figure out what I need to do to solve this. Through a mixture of StackOverFlow and re-watching videos, I was able to solve this problem. I'm so proud of my solution that I will post this snippet on my post.

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

def cesar(start_text,shift_amount,cipher_direction):
    end_text = ""
    if cipher_direction.lower() == "decode":
        shift_amount *= -1
        for char in start_text:
            end_text += alphabet[alphabet.index(char)+shift_amount]
    elif cipher_direction.lower() == "encrypt":
        for char in start_text:
            end_text += alphabet[alphabet.index(char)+shift_amount]
    else:
        print("Incorrect")
    print(end_text)
cesar("hello",5,"encrypt")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

All DEV content is created by the community!

Hey, if you're landing here for the first time, you should know that this website is a global community of folks who blog about their experiences to help folks like you out.

Sign up now if you're curious. It's free!