Following is the code I am running -
def truncate(n):
multiplier = 10
return int(n*multiplier) / multiplier
def getTotals(categories):
total = 0
breakdown =
for category in categories:
total += category.get_withdrawals()
breakdown.append(category.get_withdrawals())
rounded = list(map(lambda x: truncate(x/total), breakdown))
return rounded
def create_spend_chart(categories):
res = “Percentage spent by category\n”
i = 100
totals = getTotals(categories)
while i >= 0:
cat_spaces = " "
for total in totals:
if total * 100 >= i:
cat_spaces += “o "
else:
cat_spaces += " "
res+= str(i).rjust(3) + “|” + cat_spaces + (”\n")
i-=10
dashes = “-” + “—”*len(categories)
names =
x_axis = “”
for category in categories:
names.append(category.name)
maxi = max(names, key=len)
for x in range(len(maxi)):
nameStr = ’ ’
for name in names:
if x >= len(name):
nameStr += " "
else:
nameStr += name + " "
if(x != len(maxi) -1 ):
nameStr += '\n'
x_axis += nameStr
res+= dashes.rjust(len(dashes)+4) + “\n” + x_axis
return res
class Category:
def init(self, name):
self.name = name
self.ledger = list()
def str(self):
title = f"{self.name:*^30}\n"
items = “”
total = 0
for i in range(len(self.ledger)):
items += f"{self.ledger[i][‘description’][0:23]:23}" + f"{self.ledger[i][‘amount’]:>7.2f}" + ‘\n’
total += self.ledger[i][‘amount’]
output = title + items + "Total: " + str(total)
return output
def deposit (self, amount, description = “”):
self.ledger.append({“amount”:amount, “description”:description})
def withdraw (self, amount, description=""):
if(self.check_funds(amount)):
self.ledger.append({“amount”: -amount, “description”: description})
return True
return False
def get_balance(self):
total_cash = 0
for item in self.ledger:
total_cash += item[“amount”]
return total_cash
def transfer(self, amount, category):
if (self.check_funds(amount)):
self.withdraw(amount,"Transfer to " + category.name)
category.deposit(amount,"Transfer from " + self.name)
return True
return False
def check_funds(self, amount):
if(self.get_balance() >= amount):
return True
return False
category method
def get_withdrawals(self):
total = 0
for item in self.ledger:
if item[“amount”] < 0:
total += item[“amount”]
return total