The Spy Life

wordcloud

Note: This page contains a small but interesting piece of Python code which I call snippets. You can find more such codes on my Python snippets page.

Problem

You are a secret agent, and you receive an encrypted message that needs to be decoded. The code that is being used flips the message backwards and inserts non-alphabetic characters in the message to make it hard to decipher.

Task:
Create a program that will take the encoded message, flip it around, remove any characters that are not a letter or a space, and output the hidden message.

Input Format:
A string of characters that represent the encoded message.

Output Format:
A string of character that represent the intended secret message.

Sample Input:
d89%l++5r19o7W *o=l645le9H

Sample Output:
Hello World

Solution

Here is my solution to the above problem. Remember that there could be more than one way to solve a problem. If you have a more efficient or concise solution, please leave a comment.

In [1]:
import re
print("".join(re.findall('[a-zA-Z\s]',input()[::-1])))
Hello World

Explanation

My approach (or the algorithm)

  1. Reverse the string.
  2. Search for letters and spaces.
  3. Join them into a string.

The code

  • input()[::-1] reverses the input string.
  • '[a-zA-Z\s]' RegEx pattern to find letters and spaces.
  • "".join() joins letters into a string.

The problem question is picked from SoloLearn. Here is my SoloLearn code and my SoloLearn profile page.

Last updated 2021-01-09 22:14:45.989461 IST

Comments