-
-
Save parzibyte/2fccd949f1e82e741ee4d5e2cb736375 to your computer and use it in GitHub Desktop.
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
# https://parzibyte.me/blog | |
NOMBRE_ARCHIVO = "datos.csv" | |
NOMBRE_ARCHIVO_ESCRITURA = "documento.txt" | |
def obtener_lista_facturacion(): | |
lista = [] | |
with open(NOMBRE_ARCHIVO) as archivo: | |
next(archivo) | |
for linea in archivo: | |
datos_separados = linea.split(";") | |
año = int(datos_separados[0]) | |
trimestre1 = float(datos_separados[1]) | |
trimestre2 = float(datos_separados[2]) | |
trimestre3 = float(datos_separados[3]) | |
trimestre4 = float(datos_separados[4]) | |
lista.append({ | |
"año": año, | |
"trimestre1": trimestre1, | |
"trimestre2": trimestre2, | |
"trimestre3": trimestre3, | |
"trimestre4": trimestre4, | |
}) | |
return lista | |
def imprimir_años_con_media(): | |
lista = obtener_lista_facturacion() | |
for elemento in lista: | |
año = elemento["año"] | |
media = (elemento["trimestre1"] + elemento["trimestre2"] + | |
elemento["trimestre3"] + elemento["trimestre4"]) / 4 | |
print(f"Año {año} facturación media de {media} euros") | |
def burbuja(arreglo): | |
longitud = len(arreglo) | |
for i in range(longitud): | |
for indice_actual in range(longitud - 1): | |
indice_siguiente_elemento = indice_actual + 1 | |
if arreglo[indice_actual]["año"] < arreglo[indice_siguiente_elemento]["año"]: | |
arreglo[indice_siguiente_elemento], arreglo[indice_actual] = arreglo[indice_actual], arreglo[indice_siguiente_elemento] | |
def guardar_años_cronologicamente(): | |
lista = obtener_lista_facturacion() | |
burbuja(lista) | |
with open(NOMBRE_ARCHIVO_ESCRITURA, "w", encoding="utf-8") as archivo: | |
for elemento in lista: | |
año = elemento["año"] | |
total = (elemento["trimestre1"] + elemento["trimestre2"] + | |
elemento["trimestre3"] + elemento["trimestre4"]) | |
archivo.write(f"Año {año} facturación anual de: {total} euros.\n") | |
def main(): | |
opciones = "1. Imprimir años con media\n2. Guardar años cronológicamente\n3. Salir\nElige: " | |
eleccion = "" | |
while eleccion != "3": | |
eleccion = input(opciones) | |
if eleccion == "1": | |
imprimir_años_con_media() | |
elif eleccion == "2": | |
guardar_años_cronologicamente() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment