forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reto mouredev#11 - URL PARAMS [Fácil]
- Loading branch information
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
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,38 @@ | ||
""" | ||
/* | ||
* Dada una URL con parámetros, crea una función que obtenga sus valores. | ||
* No se pueden usar operaciones del lenguaje que realicen esta tarea directamente. | ||
* | ||
* Ejemplo: En la url https://retosdeprogramacion.com?year=2023&challenge=0 | ||
* los parámetros serían ["2023", "0"] | ||
*/ | ||
""" | ||
|
||
def obtener_parametros_url(url): | ||
parametros = [] | ||
en_parametros = False | ||
valor_actual = "" | ||
|
||
for caracter in url: | ||
if caracter == "?": | ||
en_parametros = True | ||
elif en_parametros and caracter in ["=", "&"]: | ||
if valor_actual: | ||
parametros.append(valor_actual) | ||
valor_actual = "" | ||
elif en_parametros: | ||
valor_actual += caracter | ||
print(f"caracter: {caracter}, valor_actual: {valor_actual}") | ||
|
||
if valor_actual: | ||
parametros.append(valor_actual) | ||
|
||
parametros = parametros[1::2] | ||
return parametros | ||
|
||
if __name__ == "__main__": | ||
# Ejemplo de uso | ||
url_ejemplo = "https://retosdeprogramacion.com?year=2023&challenge=0" | ||
parametros_obtenidos = obtener_parametros_url(url_ejemplo) | ||
|
||
print("Parámetros en la URL:", parametros_obtenidos) |