From 618ecac39cc0ed6f552a178c235f00df021c7745 Mon Sep 17 00:00:00 2001 From: KevinED11 Date: Tue, 25 Jul 2023 00:21:37 -0700 Subject: [PATCH] reto #30 - python --- .../python/KevinED11.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Retos/Reto #30 - EL TECLADO T9 [Media]/python/KevinED11.py diff --git a/Retos/Reto #30 - EL TECLADO T9 [Media]/python/KevinED11.py b/Retos/Reto #30 - EL TECLADO T9 [Media]/python/KevinED11.py new file mode 100644 index 0000000000..408deab052 --- /dev/null +++ b/Retos/Reto #30 - EL TECLADO T9 [Media]/python/KevinED11.py @@ -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()