Skip to content

Commit b771cd2

Browse files
authored
Merge pull request #6803 from Maanghel/main
#11 - Python
2 parents 640d776 + fa8e1e5 commit b771cd2

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
Dada una URL con parámetros, crea una función que obtenga sus valores.
3+
No se pueden usar operaciones del lenguaje que realicen esta tarea directamente.
4+
5+
Ejemplo: En la url https://retosdeprogramacion.com?year=2023&challenge=0
6+
los parámetros serían ["2023", "0"]
7+
"""
8+
9+
from typing import List
10+
11+
def url_parameters(url: str) -> List[str]:
12+
"""
13+
Extracts parameter values from a given URL string.
14+
15+
Args:
16+
url (str): The input URL.
17+
18+
Returns:
19+
List[str]: A list of parameter values found in the URL.
20+
21+
Raises:
22+
TypeError: If the URL is not a string.
23+
ValueError: If the URL is empty.
24+
"""
25+
_validate_url(url)
26+
27+
parameters = []
28+
start = None
29+
for i, c in enumerate(url):
30+
if c == '?':
31+
start = i + 1
32+
break
33+
if start is None:
34+
return parameters
35+
36+
current = start
37+
while current < len(url):
38+
eq_index = _search_eq_index(current, url)
39+
if eq_index is None:
40+
return parameters
41+
42+
amp_index = _search_amp_index(eq_index, url)
43+
data = url[eq_index + 1: amp_index]
44+
parameters.append(data)
45+
current = amp_index + 1
46+
47+
return parameters
48+
49+
def _search_eq_index(current_index: int, url: str) -> int | None:
50+
"""
51+
Searches for the next '=' symbol in the URL starting from the given index.
52+
53+
Args:
54+
current_index (int): The index to start searching from.
55+
url (str): The input URL.
56+
57+
Returns:
58+
int | None: The index of '=' if found, otherwise None.
59+
"""
60+
for i in range(current_index, len(url)):
61+
if url[i] == '=':
62+
return i
63+
return None
64+
65+
def _search_amp_index(eq_index: int, url: str) -> int:
66+
"""
67+
Searches for the next '&' symbol in the URL starting after the '=' index.
68+
69+
Args:
70+
eq_index (int): The index of the last '='.
71+
url (str): The input URL.
72+
73+
Returns:
74+
int: The index of '&' if found, otherwise the length of the URL.
75+
"""
76+
for i in range(eq_index + 1, len(url)):
77+
if url[i] == '&':
78+
return i
79+
return len(url)
80+
81+
def _validate_url(url: str) -> None:
82+
"""
83+
Validates that the input is a non-empty string.
84+
85+
Args:
86+
url (str): The URL to validate.
87+
88+
Raises:
89+
TypeError: If the URL is not a string.
90+
ValueError: If the URL is empty.
91+
"""
92+
if not isinstance(url, str):
93+
raise TypeError("La URL ingresada debe ser una cadena de texto.")
94+
if not url:
95+
raise ValueError("Debe ingresar una URL para proseguir.")
96+
97+
98+
if __name__ == "__main__":
99+
URL: str = "https://retosdeprogramacion.com?year=2023&challenge=0"
100+
print(url_parameters(URL))

0 commit comments

Comments
 (0)