Skip to content

Instantly share code, notes, and snippets.

@Kwisses
Created March 3, 2016 02:53
Show Gist options
  • Save Kwisses/b382a4427bc21f2726c0 to your computer and use it in GitHub Desktop.
Save Kwisses/b382a4427bc21f2726c0 to your computer and use it in GitHub Desktop.
[Daily Challenge #3 - Easy] - Caesar Cipher - r/DailyProgrammer
# ***** DAILY CHALLENGE #3 - Easy *****
# Welcome to cipher day!
# Write a program that can encrypt texts with an alphabetical caesar cipher.
# This cipher can ignore numbers, symbols, and whitespace.
#
# for extra credit, add a "decrypt" function to your program!
# ---------------------------------------------------------------------------------------------------------------------
from string import ascii_lowercase
alpha = ascii_lowercase
def cipher(text, shift):
"""Shifts text by x amount thereby coding it.
Args:
text (str): Text to shift
shift (int): Shifts text by this amount
Return:
str: Text shifted in alphabet by x amount.
"""
output = []
for letter in text:
if letter.lower() in alpha:
if alpha.index(letter.lower()) + shift >= 26:
shift -= 26
output.append(alpha[alpha.index(letter.lower()) + shift])
else:
output.append(alpha[alpha.index(letter.lower()) + shift])
return ''.join(output)
def decrypt(code, shift):
"""Decodes caesar cipher text by shifting alphabet by x amount.
Args:
code (str): Takes an already coded caesar cipher text.
shift (int): Shifts code by this amount
Return:
str: Code shifted in alphabet by x amount.
"""
output = []
for letter in code:
output.append(alpha[alpha.index(letter) - shift])
return ''.join(output)
string = input("Enter Text Here: ")
number = 15
cipher = cipher(string, number)
decipher = decrypt(cipher, number)
print(cipher)
print(decipher)
@Kwisses
Copy link
Author

Kwisses commented Mar 3, 2016

[Daily Challenge #3 - Easy] from the DailyProgrammer subreddit. Caesar Cipher with bonus decipher function.

Reference: https://www.reddit.com/r/dailyprogrammer/comments/pkw2m/2112012_challenge_3_easy/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment