OiO.lk Blog python I want to solve this question in Python, but I couldn't include all the solutions. I tried hard, but I don't know what the problem is
python

I want to solve this question in Python, but I couldn't include all the solutions. I tried hard, but I don't know what the problem is


You are given a string of encrypted text (ciphertext).
The encryption algorithm used to create the ciphertext simply shifts all the alphabetic characters in the original (unencrypted) string by the same amount. But you don”t know what this amount is.
Write the decipher function that takes the encrypted string as input, and returns the original, unencrypted string.
For example, imagine that the original message was "hello" and we shifted each letter by two. The resulting ciphertext would be "jgnnq".
If the original message were "Coding tests are fun and challenging!" and we shifted each character by two, the resulting ciphertext would be "Eqfkpi vguvu ctg hwp cpf ejcnngpikpi!"
The decipher function takes two arguments: the ciphertext, and a word that we know appeared in the original plain text. Using these clues, the function must output the original text.
We will follow the English alphabet for this question. Note that the last letter of the alphabet Z will be followed by A. Likewise, z will be followed by a.
If the word you are searching for in the original string does not appear there, return "Invalid".
Example 1
Input:
"Eqfkpi vguvu ctg hwp!"
"tests"
Output:
"Coding tests are fun!"
Explanation:
"tests" is a five-letter word. In the encrypted string, the only five-letter word is "vguvu". Therefore the encrypted version of "tests" may be "vguvu". On comparing "tests" to "vguvu", it is clear that the encryption process has shifted every character in the plaintext by 2. So, the plaintext in this case is "Coding tests are fun!".
Example 2
Input:
"cdeb nqxg"
"love"
Output:
"abcz love"
Explanation:
In this case, "love" could have been encrypted to either "cdeb" or "nqxg". On closer examination, it is clear that "nqxg" is the correct option, with every character shifted by 2. (No such relationship exists between "love" and "cdeb")

import re

def decipher(ciphertext, known_word):
def shift_char(c, shift):
if ‘a’ <= c <= ‘z’:
return chr((ord(c) – ord(‘a’) – shift) % 26 + ord(‘a’))
elif ‘A’ <= c <= ‘Z’:
return chr((ord(c) – ord(‘A’) – shift) % 26 + ord(‘A’))
else:
return c

cleaned_known_word = re.sub(r'[\W\d]+', '', known_word)

words = re.findall(r'\b\w+\b|\S', ciphertext)

for word in words:
    for shift in range(26): 
        cleaned_word = re.sub(r'[\W\d]+', '', word)
        if len(cleaned_word) == len(cleaned_known_word):
            possible_decryption = ''.join([shift_char(c, shift) for c in cleaned_word])

            if possible_decryption == cleaned_known_word:
                decrypted_text="".join([shift_char(c, shift) for c in ciphertext])
                return decrypted_text

return "Invalid"

ciphertext = input().strip()
knownWord = input().strip()

result = decipher(ciphertext, knownWord)
print(result)

I expected that I covered everything, but when I submitted the answer, I discovered that I did not answer the entire question. I do not know if this is the reason.



You need to sign in to view this answers

Exit mobile version