Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reto #30 - python #4292

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()