Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# OpenBootCampPython
Repository to store Python boot camp.
* Ejercicio 9.B: En este segundo ejercicio, tenéis que crear una aplicación que obtendrá los elementos impares de una lista pasada por parámetro con filter y realizará una suma de todos estos elementos obtenidos mediante reduce.
75 changes: 75 additions & 0 deletions src/exercisenine/sumarimpares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from functools import reduce


def isNumeric(lista: list[str]) -> bool:
"""Check if a list of strings are numeric
:param lista: list of strings to check
:type lista: list[str]
:return: true if all elements in the list are numeric, false otherwise
:rtype: bool
"""
return all(x.isnumeric() for x in lista)


def strToInt(lista: list[str]) -> list[int]:
"""Transform a list of strings to a list of integers
:param lista: list of strings to transform
:type lista: list[str]
:return: list of strings transformed in integer
:rtype: list[int]
"""
return list(int(x) for x in lista)


def isImpar(a: int) -> bool:
"""Check if a number is odd.
:param a: number to check
:type a: int
:return: true if the numbers is odd, false otherwise
:rtype: bool
"""
if a % 2 != 0:
return True
return False


def getImpares(lista: list[int]) -> list[int]:
"""Function that filter a list of integers and take odd numbers
:param lista: list of integers to filter
:type lista: list[int]
:return: list filtered with odd numbers
:rtype: list[int]
"""
return list(filter(isImpar, lista))


def sumar(a: int, b: int) -> int:
"""Function to add two numbers
:param a: first operand
:type a: int
:param b: second operand
:type b: int
:return: addition of the operands
:rtype: int
"""
return a + b


def sumarImpares(lista: list[str]) -> tuple[int, int, list[int]]:
"""Function that check if a list of strings are numerics,
then check if the list is not empty, take the odd numbers of the list,
finally make arithmetic addition with all odd numbers of the list
:param lista: list of integers
:type lista: list[str]
:return: error -> 0 if correct, 1 if the list is empty, 2 if the list is not numeric
result of the operation
list of the operands
:rtype: tuple[int, int, list[int]]
"""
if isNumeric(lista):
lista = getImpares(strToInt(lista))
if len(lista) != 0:
return 0, reduce(sumar, lista), lista
else:
return 1, 1, []
return 2, 1, []
41 changes: 35 additions & 6 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
def main():
"""Main script for pythonBootCamp project
:return: None
:rtype: NoneType
from sys import argv

from exercisenine.sumarimpares import sumarImpares


def showCalculation(lista: list[int], resultado: int):
"""Function that show in terminal the operation
:param lista: list of digit to operate
:type lista: list[int]
:param resultado: result of the operation
:type resultado: int
"""
msg = str(lista[0])
for digito in lista[1:len(lista)]:
msg += ' + '
msg += str(digito)
print(f'{msg} = {resultado}')


def main(args, argc):
"""Main script for exercise nine B.
"""
pass
if argc < 1:
print('Introduzca algún digito entero')
exit(1)
error, resultado, impares = sumarImpares(args)
if error == 0:
showCalculation(impares, resultado)
exit(error)
elif error == 1:
print('No hay elementos que sumar')
exit(error)
elif error == 2:
print('Error, hay elementos no numéricos')
exit(error)


if __name__ == '__main__':
main()
main(argv[1:len(argv)], len(argv[1:len(argv)]))