Skip to content

Commit c1e751e

Browse files
authored
Merge pull request #6806 from Maanghel/main
#15/16 - Python
2 parents d16fce9 + 27494e7 commit c1e751e

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
"""
2+
Crea una función que sea capaz de transformar Español al lenguaje básico
3+
del universo Star Wars: el "Aurebesh".
4+
- Puedes dejar sin transformar los caracteres que no existan en "Aurebesh".
5+
- También tiene que ser capaz de traducir en sentido contrario.
6+
7+
¿Lo has conseguido? Nómbrame en twitter.com/mouredev y escríbeme algo en Aurebesh.
8+
9+
¡Que la fuerza os acompañe!
10+
"""
11+
12+
class Aurebesh():
13+
"""
14+
Translator between Spanish text and the Aurebesh alphabet
15+
(the written form of Galactic Basic in the Star Wars universe).
16+
17+
Features:
18+
- Provides bidirectional translation:
19+
* from Spanish to Aurebesh (`to_aurebesh`)
20+
* from Aurebesh to Spanish (`to_espanol`)
21+
- Unknown characters (not present in Aurebesh) are preserved as-is.
22+
- Handles the special case "ch" as a single Aurebesh token.
23+
"""
24+
25+
mapping_aurebesh: dict[str, str] = {
26+
"a": "Aurek", "b": "Besh", "c": "Cresh", "d": "Dorn", "e": "Enth",
27+
"f": "Forn", "g": "Grek", "h": "Herf", "i": "Isk", "j": "Jenth",
28+
"k": "Krill", "l": "Leth", "m": "Mern", "n": "Nern", "o": "Osk",
29+
"p": "Peth", "q": "Qek", "r": "Resh", "s": "Senth", "t": "Trill",
30+
"u": "Usk", "v": "Vev", "w": "Wesk", "x": "Xesh", "y": "Yirt", "z": "Zerek",
31+
"ch": "Cherek", ' ': ''
32+
}
33+
34+
def to_aurebesh(self, text: str) -> str:
35+
"""
36+
Convert a Spanish string into Aurebesh.
37+
38+
Args:
39+
text (str): Input text in Spanish.
40+
41+
Returns:
42+
str: The corresponding text in Aurebesh. Characters not present in the mapping
43+
will remain unchanged.
44+
45+
Notes:
46+
- Multi-character mappings (e.g., "ch") are handled first.
47+
- Output tokens are separated by spaces.
48+
"""
49+
50+
self._validate_language(text)
51+
text_aurebesh: list[str] = []
52+
text = text.lower()
53+
count: int = 0
54+
while count < len(text):
55+
if count + 1 < len(text) and text[count:count + 2] in self.mapping_aurebesh:
56+
text_aurebesh.append(self.mapping_aurebesh[text[count:count + 2]])
57+
count += 2
58+
elif text[count] in self.mapping_aurebesh:
59+
text_aurebesh.append(self.mapping_aurebesh[text[count]])
60+
count += 1
61+
else:
62+
text_aurebesh.append(text[count])
63+
count += 1
64+
return " ".join(text_aurebesh)
65+
66+
def _validate_language(self, text: str, aurebesh: bool=False) -> None:
67+
"""
68+
Validate that the input string is in the expected language format.
69+
70+
Args:
71+
text (str): Input text to validate.
72+
aurebesh (bool): If True, checks that the text only contains valid Aurebesh tokens.
73+
If False, performs a basic Spanish validation.
74+
75+
Raises:
76+
TypeError: If the input is not a string.
77+
ValueError: If the string is empty or contains invalid tokens for the given language.
78+
"""
79+
if not isinstance(text, str):
80+
raise TypeError("Solo se aceptan cadenas de texto.")
81+
if not text:
82+
raise ValueError("El texto no puede estar vacio.")
83+
84+
if aurebesh:
85+
words = text.split()
86+
for word in words:
87+
if word not in self.mapping_aurebesh.values():
88+
raise ValueError("El texto debe estar en aurebesh.")
89+
90+
def to_espanol(self, text_aurebesh: str) -> str:
91+
"""
92+
Convert an Aurebesh string back into Spanish.
93+
94+
Args:
95+
text_aurebesh (str): Input text in Aurebesh, with tokens separated by spaces.
96+
97+
Returns:
98+
str: The corresponding text in Spanish.
99+
100+
Raises:
101+
ValueError: If the input is not valid Aurebesh.
102+
103+
Notes:
104+
- Unknown Aurebesh tokens are replaced with "?".
105+
- Empty string validation is enforced.
106+
"""
107+
self._validate_language(text_aurebesh, aurebesh=True)
108+
reverse_aurebesh = {v: k for k, v in self.mapping_aurebesh.items()}
109+
words = text_aurebesh.split(" ")
110+
text_spanish = [reverse_aurebesh.get(word, "?") for word in words]
111+
return "".join(text_spanish)
112+
113+
114+
if __name__ == "__main__":
115+
aurebesh = Aurebesh()
116+
print(aurebesh.to_aurebesh("Hola mundo!!"))
117+
print(aurebesh.to_espanol("Herf Osk Leth Aurek Mern Usk Nern Dorn Osk"))
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Crea una función que dibuje una escalera según su número de escalones.
3+
- Si el número es positivo, será ascendente de izquierda a derecha.
4+
- Si el número es negativo, será descendente de izquierda a derecha.
5+
- Si el número es cero, se dibujarán dos guiones bajos (__).
6+
Ejemplo: 4
7+
* _
8+
* _|
9+
* _|
10+
* _|
11+
* _|
12+
"""
13+
14+
def stairs(num_steps: int) -> None:
15+
"""
16+
Draws a staircase in the console based on the given number of steps.
17+
18+
- Positive values create an ascending staircase (left to right).
19+
- Negative values create a descending staircase (left to right).
20+
- Zero prints two underscores (__).
21+
22+
Args:
23+
num_steps (int): Number of steps in the staircase.
24+
"""
25+
_validate_steps(num_steps)
26+
if num_steps == 0:
27+
print("__")
28+
elif num_steps > 0:
29+
print(f"{'_':>{(num_steps * 2) + 1}}")
30+
for i in range(num_steps * 2, 0, -2):
31+
print(f"{'_|':>{i}}")
32+
else:
33+
print("_")
34+
for i in range(1, abs(num_steps * 2) + 1, 2):
35+
print(f"{'|_':>{i + 2}}")
36+
37+
def _validate_steps(steps: int) -> None:
38+
"""
39+
Validate that the given input is an integer.
40+
41+
Args:
42+
steps (int): The number of steps to validate.
43+
44+
Raises:
45+
TypeError: If the input is not an integer.
46+
"""
47+
if not isinstance(steps, int):
48+
raise TypeError("Solo se aceptan enteros como datos.")
49+
50+
51+
if __name__ == "__main__":
52+
stairs(-6)
53+
stairs(6)

0 commit comments

Comments
 (0)