Each letter in the alphabet can be given a value based on its position in the alphabet, A being 1 throughto Z being 26. Two letters can produce a third letter by adding together their values, deducting 26 fromthe sum if it is greater than 26, and finding the letter whose value equals this result.A simple encryption scheme for a string is as follows. Working from left to right, after leaving the left-most character alone, each character is replaced with the character produced by adding it to thecharacter to its immediate left.For example:• The string ENCRYPT goes through the steps ENCRYPT → ESCRYPT → ESVRYPT → ESVNYPT →ESVNMPT -> ESVNMCT -> ESVNMCW1(a) [ 24 marks ]Write a program that reads in an encrypted string, between 1 and 10 uppercaseletters inclusive, and outputs the original version of that string
时间: 2024-01-17 22:05:51 浏览: 87
mbmbalphabet:http
Here's a Python program that implements the encryption scheme and decrypts the input string:
```python
def decrypt(s):
# convert string to list of ASCII codes
code = [ord(c) - 64 for c in s]
# apply decryption formula
for i in range(2, len(code)):
code[i] = (code[i] - code[i-1]) % 26
# convert back to string
return ''.join(chr(c + 64) for c in code)
# read input string
s = input().strip()
# decrypt and print result
print(decrypt(s))
```
The `decrypt` function takes a string `s`, converts it to a list of ASCII codes (where A=1, B=2, etc.), applies the decryption formula to each code, and converts the result back to a string. The formula subtracts the code of the previous character from the current code, and takes the result modulo 26 (to handle cases where the sum is greater than 26).
The main program reads the input string (which is assumed to be a single line of uppercase letters), calls the `decrypt` function, and prints the result.
阅读全文