Symbols

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

There is a problem with your keyboard: it randomly writes symbols when you are typing a text. You need to clean up the text by removing all symbols.

Task:
Take a text that includes some random symbols and translate it into a text that has none of them. The resulting text should only include letters and numbers.

Input Format:
A string with random symbols.

Output Format:
A string of the text with all the symbols removed.

Sample Input:

l\$e%ts go @an#d@@ g***et #l#unch$$$

Sample Output:
lets go and get lunch

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('[\w\s]',input())))
lets go and get lunch

Explanation

My approach (or the algorithm)

  • Use RegEx to find alphanumeric characters and spaces.
  • Join them into a string.

The code

  • '[\w\s]' RegEx pattern. \w searches for alphanumeric characters. \s searches for spaces.
  • "".join() joins all the matches 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:25:05.764668 IST

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

Security Alarm

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 in charge of security at a casino, and there is a thief who is trying to steal the casino's money! Look over the security diagrams to make sure that you always have a guard between the thief and the money! There is one money location, one thief, and any number of guards on each floor of the casino.

Task:
Evaluate a given floor of the casino to determine if there is a guard between the money and the thief, if there is not, you will sound an alarm.

Input Format:
A string of characters that includes $ (money), T (thief), and G (guard), that represents the layout of the casino floor.
Space on the casino floor that is not occupied by either money, the thief, or a guard is represented by the character x.

Output Format:
A string that says 'ALARM' if the money is in danger or 'quiet' if the money is safe.

Sample Input:
xxxGxxGxx$xxxTG

Sample Output:
ALARM

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]:
i=input().replace('x','')
if abs(i.index('$')-i.index('T'))==1:
    print('ALARM')
else:
    print('quiet')
ALARM

Explanation

My approach (or the algorithm)

  1. Remove the noise in the data. We are only interested in the floors with \$ (money), T (thief), and G (guard). Delete all "x".
  2. If there is no G between \$ and T then \$ and T will be adjacent to each other. The distance between the two will be 1. If so, then sound alarm, else quiet.

The code

  • replace('x','') remove unnecessary data (all "x"s).
  • abs(i.index('$')-i.index('T')) distance between \$ and T.

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

Last updated 2021-01-09 22:02:23.522813 IST

Secret Message

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 trying to send a secret message, and you've decided to encode it by replacing every letter in your message with its corresponding letter in a backwards version of the alphabet. What do your messages look like?

Task:
Create a program that replaces each letter in a message with its corresponding letter in a backwards version of the English alphabet.

Input Format:
A string of your message in its normal form.

Output Format:
A string of your message once you have encoded it (all lower case).

Sample Input:
Hello World

Sample Output:
svool dliow

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 string
az=string.ascii_lowercase
print("".join([az[-az.index(x)-1] if x in az else x for x in input().lower()]))
svool dliow!

Explanation

My approach (or the algorithm)

  1. Convert the input to lowercase.
  2. Create a list of lowercase letters from a to z.
  3. Find the position of each letter of the input string in the alphabet list.
  4. Find a corresponding letter in the alphabet list with the same position from the end of the list.
  5. Replace each letter from input string with the corresponding letter found in the alphabet llst.

The code

  • input().lower() converts input to lowercase.
  • if x in az we will make changes only if the letter is an alphabet and not a number or a special character.
  • az.index(x) gives the position of the input letter in the alphabet list.
  • -az.index(x)-1 gives the position of the letter in the alphabet list from the end.
  • [az[-az.index(x)-1] if x in az else x for x in input().lower()] replace all letters using list comprehension.
  • "".join() join 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 18:18:58.213994 IST