Tell us what’s happening:
test case 19, 20, 21 seems to me covered but not really passing by test case checker!!
where it needs some changes for completing this test cases…
thanks for looking into this much appreciated
Your code so far
import math
class Category:
def __init__(self, name):
self.ledger = []
self.name = name
def get_ledger(self):
return self.ledger
def get_name(self):
return self.name
def deposit(self, amount, descr=''):
self.ledger.append({'amount': amount, 'description': descr})
def withdraw(self, amount, descr=''):
if self.check_funds(amount):
self.ledger.append({'amount': -amount, 'description': descr})
return True
else:
return False
def get_balance(self):
s = 0
for item in self.ledger:
s += item['amount']
return s
def check_funds(self, amount):
return amount <= self.get_balance()
def transfer(self, amount, dest_category):
if self.check_funds(amount):
withdraws = self.withdraw(amount, f"Transfer to {dest_category.name}")
deposits = dest_category.deposit(amount, f"Transfer from {self.name}")
return True
else:
return False
def decorate_category(self):
left_pad = (30 - len(self.name)) // 2
right_pad = (30 - len(self.name)) - left_pad
dec_tit = "*" * left_pad + self.name + "*" * right_pad
# return(dec_tit +"\n")
return(dec_tit)
def print_ledger(self):
line = ''
for item in self.ledger:
a, d = item
amt = item[a]
des = item[d]
chk_left = 23 - len(des)
amt_ra = "{:.2f}".format(amt).rjust(7)
des_la = ""
if chk_left < 0:
des_la = des[0:23]
else:
des_la = des.ljust(23)
# print(des_la + amt_ra)
line += (des_la + amt_ra) + "\n"
return line
def print_ledger_total(self):
sum = 0
for item in self.ledger:
a,d = item
sum += item[a]
return(f"Total: {sum}")
def __str__(self):
# Build the string representation
return f"{self.decorate_category()}\n{self.print_ledger()}{self.print_ledger_total()}"
def create_spend_chart(categories):
withdrawals = {}
for category in categories:
total = 0
for transaction in category.get_ledger():
amount, descr = transaction
amt = transaction[amount]
if(amt < 0):
total += (-amt)
tot = total
withdrawals[category] = tot
percentages = {}
total_withdrawals = sum(withdrawals.values())
def custom_round_to_nearest_10(x, threshold=0.5):
"""
Rounds a number to the nearest 10.
If the fractional part of (x/10) is >= threshold, it rounds up.
Otherwise, it rounds down.
"""
base = int(x // 10) * 10
remainder = x % 10
if remainder / 10 >= threshold:
return base + 10
else:
return base
for category, withdrawal in withdrawals.items():
# 1. Calculate the percentage relative to the TOTAL withdrawals, not 100
# (Assuming total_withdrawals is calculated beforehand)
perc = (withdrawal / total_withdrawals) * 100
print(perc, "perc")
# 2. Force round DOWN to the nearest 10 using integer division
calc = custom_round_to_nearest_10(perc)
print(calc, "calc")
percentages[category.get_name()] = calc
def top_side():
levels = [100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0]
chart_lines = []
for v in levels:
# Format the single Y-axis label to align nicely (3 spaces wide)
row = f"{v:>3}|"
# Check every category at this specific height level
for name, amt in percentages.items():
# If the category amount is greater or equal, draw the bar segment
if amt >= v:
row += "o "
else:
row += ""
if v==0:
# row += f" {name} "
row += " "
chart_lines.append(row)
return "\n".join(chart_lines) + "\n"
divs = ("---" * len(percentages.keys())).rjust(10)
# divs = ("---" * len(percentages.keys()) + "--").rjust(12)
cn = ''
for category, withdrawal in withdrawals.items():
print(category)
return("Percentage spent by category\n" +top_side() + divs)
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36
Challenge Information:
Build a Budget App - Build a Budget App





