diff --git a/README.md b/README.md index fa0237d..6363ba4 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # OpenBootCampPython Repository to store Python boot camp. + +* Ejercicio 9.A: Crea un script que le pida al usuario una lista de países (separados por comas). Éstos se deben almacenar en una lista. No debería haber países repetidos (haz uso de set). Finalmente, muestra por consola la lista de países ordenados alfabéticamente y separados por comas. diff --git a/src/exercisenine/countries.py b/src/exercisenine/countries.py new file mode 100644 index 0000000..7d14278 --- /dev/null +++ b/src/exercisenine/countries.py @@ -0,0 +1,49 @@ +def noContieneNumeros(lista: list[str]) -> bool: + """Check if a list has alphabetic characters, white space included + :param lista: list of strings + :type lista: + :return: true if all elements are alphabetic characters + :rtype: bool + """ + return all(x.replace(' ', '').isalpha() for x in lista) + + +def transformarLista(paises: list[str]) -> list[str]: + """Function that sort the list of strings, + remove repeated elements and capitalize the first character of the string + :param paises: string list to be treated + :type paises: list[str] + :return: transformed string list + :rtype: list[str] + """ + paises = sorted(list(x.lower() for x in paises)) + paises = list(set(paises)) + paises = list(x.title() for x in paises) + return paises + + +def inputPaises() -> list[str]: + """Function that take user input separated by coma and return a list of strings + :return: the user input in a list of strings + :rtype: list[str] + """ + paises = input('Escribe una lista de paises separados por coma: ').split(sep=',') + return list(x.strip() for x in paises) + + +def getPaises() -> tuple[int, list[str]]: + """Function that take a list of countries from user, + check if it has alphabetic characters, + apply a data transform, then return if it has errors and the list of strings. + :return: a tuple where the first element is 0 if there is error in the process + or 1 if correct. The second element is the list of strings. + :rtype: tuple[int, list[str]] + """ + paises = list() + temp = inputPaises() + if noContieneNumeros(temp): + paises.extend(temp) + transformarLista(paises) + return 0, paises + else: + return 1, temp diff --git a/src/main.py b/src/main.py index a47b6a3..b24f263 100644 --- a/src/main.py +++ b/src/main.py @@ -1,10 +1,29 @@ +from exercisenine.countries import getPaises + + +def mostrarPaises(paises: list[str]): + """Show in terminal the list of countries separated by comma. + :param paises: lista de paises + :type paises: list[str] + """ + msg = '' + msg += paises[0] + for pais in paises[1:len(paises)]: + msg += ', ' + msg += pais + print(msg) + + def main(): - """Main script for pythonBootCamp project - :return: None - :rtype: NoneType + """Main script for exercise nine A """ - pass + error, paises = getPaises() + if error == 0: + mostrarPaises(paises) + else: + print(f'Error al introducir paises: {paises}') +# Reino Unido, Italia, España, Francia if __name__ == '__main__': main()