-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4292 from KevinED11/main
reto #30 - python
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
Retos/Reto #30 - EL TECLADO T9 [Media]/python/KevinED11.py
This file contains 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
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() |