Skip to content

Instantly share code, notes, and snippets.

@MLWhiz
Created June 20, 2019 18:33
Show Gist options
  • Save MLWhiz/4906672c6add74fa8bc6f17647cf0900 to your computer and use it in GitHub Desktop.
Save MLWhiz/4906672c6add74fa8bc6f17647cf0900 to your computer and use it in GitHub Desktop.
# AIM: To Decrypt a text using MCMC approach. i.e. find decryption key which we will call cipher from now on.
import string
import math
import random
# This function takes as input a decryption key and creates a dict for key where each letter in the decryption key
# maps to a alphabet For example if the decryption key is "DGHJKL...." this function will create a dict like {D:A,G:B,H:C....}
def create_cipher_dict(cipher):
cipher_dict = {}
alphabet_list = list(string.ascii_uppercase)
for i in range(len(cipher)):
cipher_dict[alphabet_list[i]] = cipher[i]
return cipher_dict
# This function takes a text and applies the cipher/key on the text and returns text.
def apply_cipher_on_text(text,cipher):
cipher_dict = create_cipher_dict(cipher)
text = list(text)
newtext = ""
for elem in text:
if elem.upper() in cipher_dict:
newtext+=cipher_dict[elem.upper()]
else:
newtext+=" "
return newtext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment