Created
June 1, 2020 07:57
-
-
Save kurzweil777/d7621aa11804b30d6fc7e0a2396247d1 to your computer and use it in GitHub Desktop.
Exercise_from_CodeWars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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