135k views
0 votes
Solve this with Python

M is transformed into M* by swapping every character in odd position with the immediately following character.
M* is transformed into M by swapping each character with its specular image. That is, the character in the first position is swapped with the last one, the second one with the penultimate, and so on.
For example, for M = "h o l a m u n d o",
M* would be = "o h a l m n u o d",
and M would be = "d o u n m l a h o".

Given a series of messages encrypted in this system, could you write a program to decrypt them?

Input:
The input begins with a line containing a positive integer C that corresponds to the number of test cases, at most 50. Then follow C lines for each case, each with up to 2000 characters separated by a blank space.

Output:
For each test case, the output must contain a line with the decrypted message without blank spaces.

Example Input:
2
i a a f g r t o i p c r
o i n a m c e n e e n v i 6 _ 8 t e e n a g
Example Output:
criptografia
agente_86_viene_en_camino

User Pulsar
by
8.5k points

1 Answer

3 votes

Final answer:

A Python program is presented to decrypt messages by reversing the two-step encryption process: swapping each character with its specular image, and then swapping every character in odd positions with the following character.

Step-by-step explanation:

To decrypt messages encrypted with the specified system using Python, we need to reverse the two steps of transformation described. First, we need to swap each character with its specular image, and then we swap back every character in an odd position with the immediately following character. Here is an example Python program to achieve this:
def decrypt_message(message):
# Step 1: Swap characters with their specular image
spec_swap = message[::-1]
# Step 2: Swap back characters in odd positions with the following character
char_list = list(spec_swap)
for i in range(0, len(char_list)-1, 2):
char_list[i], char_list[i+1] = char_list[i+1], char_list[i]
# Return the decrypted message as a string without spaces
return ''.join(char_list).replace(' ', '')
# Read the number of test cases
cases = int(input())
for _ in range(cases):
encrypted_message = input()
print(decrypt_message(encrypted_message))

Note that this code assumes that the input is provided in the correct format and the decrypted message is printed without spaces, as requested in the problem statement.

User Martin Booth
by
8.7k points