반응형
Welcome.
In this kata you are required to, given a string, replace every letter with its position in the alphabet.
If anything in the text isn't a letter, ignore it and don't return it.
"a" = 1, "b" = 2, etc.
Example
alphabet_position("The sunset sets at twelve o' clock.")
Should return "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" ( as a string )
Solution:
from typing import List
def alphabet_position(text: str) -> str:
alphabete = _check_alphabet(text)
positions = _get_alphabet_positions(alphabete)
alphabet_position = ' '.join(positions)
return alphabet_position
def _check_alphabet(text: str) -> List[str]:
return [i for i in text if i.isalpha()]
def _get_alphabet_positions(alphabete: List[str]) -> List[str]:
start_position = ord("a") - 1
return [str((ord(alphabet.lower())-start_position)) for alphabet in alphabete]
1. Check whether the text is alphabetic.
2. Convert Alphabet to Position Number
3. Return the converted alphabetic position number.
Best Practice:
def alphabet_position(text):
return ' '.join(str(ord(c) - ord("a")+1) for c in text.lower() if c.isalpha())
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
Check the exam (0) | 2022.07.24 |
---|---|
What is between? (0) | 2022.07.24 |
Anagram Detection (0) | 2022.07.21 |
Sum of all the multiples of 3 or 5 (0) | 2022.07.20 |
SQL Basics: Simple JOIN with COUNT (0) | 2022.07.19 |