Files
30-days-of-py/day21/excersises.py
Rodo Yamazaki cb260960c9 day 21
2026-02-12 00:53:55 -06:00

150 lines
4.3 KiB
Python

# PT1
ages = [31, 26, 34, 37, 27, 26, 32, 32, 26, 27, 27, 24, 32, 33, 27, 25, 26, 38, 37, 31, 34, 24, 33, 29, 26]
class Estadisticas:
def __init__(self, items: list):
self.items = items
def count(self):
return len(self.items)
def sum(self):
return sum(self.items)
def min(self):
return min(self.items)
def max(self):
return max(self.items)
def range(self):
return max(self.items) - min(self.items)
def mean(self):
return (sum(self.items) / len(self.items))
def median(self):
return (sorted(self.items)[(len(self.items)//2)])
def mode(self):
full_number = 0
full_mode = 0
sortedlist = sorted(self.items)
for i in range(len(sortedlist)):
current_number = sortedlist[i]
temp_mode = 0
for j in range(len(sortedlist)):
if sortedlist[j] == sortedlist[i]:
temp_mode += 1
if temp_mode > full_mode:
full_number = current_number
full_mode = temp_mode
return {'mode': full_number, 'count': full_mode}
def std(self):
new_list = []
mean = sum(self.items) / len(self.items)
for item in self.items:
new_list.append((item - mean) ** 2)
s = (sum(new_list)/(len(new_list))) ** (1/2)
return round(s,1)
def var(self):
new_list = []
mean = sum(self.items) / len(self.items)
for item in self.items:
new_list.append((item - mean) ** 2)
return round(sum(new_list)/(len(new_list)),1)
def freq_dist(self):
sortedlist = sorted(self.items)
new_list = []
current_number = 0
for i in range(len(sortedlist)):
if current_number != sortedlist[i]:
current_number = sortedlist[i]
current_count = 0
for j in range(len(sortedlist)):
if sortedlist[j] == sortedlist[i]:
current_count += 1
if (current_number,current_count) not in new_list:
new_list.append((current_number,current_count))
return new_list
def describe(self):
print('Count:', self.count()) # 25
print('Sum: ', self.sum()) # 744
print('Min: ', self.min()) # 24
print('Max: ', self.max()) # 38
print('Range: ', self.range()) # 14
print('Mean: ', self.mean()) # 30
print('Median: ', self.median()) # 29
print('Mode: ', self.mode()) # {'mode': 26, 'count': 5}
print('Standard Deviation: ', self.std()) # 4.2
print('Variance: ', self.var()) # 17.5
print('Frequency Distribution: ', self.freq_dist())
data = Estadisticas(ages)
data.describe()
# PT 2
"""
Create a class called PersonAccount. It has firstname, lastname, incomes, expenses properties and it has total_income,
total_expense, account_info, add_income, add_expense and account_balance methods. Incomes is a set of incomes and its
description. The same goes for expenses.
"""
class CuentaPersona:
def __init__(self, prim_nom, apellido):
self.prim_nom = prim_nom
self.apellido = apellido
self.ingresos = set()
self.gastos = set()
def gasto_total(self):
gastotot = 0
for item in list(self.gastos):
gasto, desc = item
gastotot += gasto
return gastotot
def anadir_ingreso(self, monto: float, info: str):
self.ingresos.add((monto, info))
def anadir_gasto(self, monto: float, info: str):
self.gastos.add((monto, info))
def balance_cuenta(self):
lista = list(self.ingresos)
suma1 = 0; suma2 = self.gasto_total()
for item in lista:
monto, desc = item
suma1 += monto
return suma1 - suma2
def informacion_cuenta(self):
print(f"Nombre: {self.prim_nom} {self.apellido}")
print(f"Balance: {self.balance_cuenta()}")
print(f"Gastos: {self.gastos}")
print(f"Gasto Total {self.gasto_total()}")
print(f"Ingresos: {self.ingresos}")
persona = CuentaPersona("Juan", "Ley")
persona.anadir_gasto(29.30, "Coca de 2 litros")
persona.anadir_ingreso(4900, "Pago")
persona.informacion_cuenta()