Skip to content

Commit

Permalink
Merge pull request #4292 from KevinED11/main
Browse files Browse the repository at this point in the history
reto #30 - python
  • Loading branch information
migueltortg authored Jul 25, 2023
2 parents da31eb7 + 618ecac commit a068045
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Retos/Reto #30 - EL TECLADO T9 [Media]/python/KevinED11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from functools import lru_cache


T9Keyboard = dict[str, str]


class InvalidT9Key(Exception):
def __init__(self, message: str) -> None:
super().__init__(message)


@lru_cache(maxsize=120)
def get_t9_keyboard() -> T9Keyboard:
return {str(n): (" ", ",.?!", "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ")[n] for n in range(10)}


def convert_t9_key_to_character(key: str) -> str:
num_pressed = key[0]
if not num_pressed in get_t9_keyboard():
raise InvalidT9Key("Enter a valid t9 key")

return get_t9_keyboard()[num_pressed][len(key) - 1]


@lru_cache(maxsize=120)
def t9_to_text(keys: str) -> str:
if not isinstance(keys, str) or not keys:
raise ValueError("Enter a T9 secuence string")

return "".join(convert_t9_key_to_character(key=key) for key in keys.split("-"))


def main() -> None:
mouredev = t9_to_text("6-666-88-777-33-3-33-888")
print(mouredev)


if __name__ == "__main__":
main()

0 comments on commit a068045

Please sign in to comment.