Skip to content

Instantly share code, notes, and snippets.

@kurzweil777
Created June 1, 2020 07:57
Show Gist options
  • Save kurzweil777/d7621aa11804b30d6fc7e0a2396247d1 to your computer and use it in GitHub Desktop.
Save kurzweil777/d7621aa11804b30d6fc7e0a2396247d1 to your computer and use it in GitHub Desktop.
Exercise_from_CodeWars
import string
import re
def alphabet_position(text):
"""This function replaces every letter with its position in the
alphabet."""
alphabet = (dict(enumerate(string.ascii_lowercase))) # Creating a dictionary with alphabet
alphabet_updated = {value: key + 1 for key, value in alphabet.items()} # Swapping keys and values
lower_text = text.lower()
lower_text = re.sub(r'[\W\d]', '', lower_text) # Getting rid of symbols and numbers
for letter in lower_text:
if letter in alphabet_updated.keys():
# Replacing the letter with its position in alphabet
lower_text = lower_text.replace(letter, str(alphabet_updated[letter]) + " ")
# Getting rid of space in the end of string
lower_text = lower_text.rstrip()
else:
pass
return lower_text
print(alphabet_position("The sunset sets at twelve o' clock."))
# 20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment