## DEV Community is a community of 636,189 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Discussion on: Daily Challenge #4 - Checkbook Balancing

Héctor Pascual

Python:

``````import re

checkbook = """1233.00
125 Hardware;! 24.8?;
123 Flowers 93.5
127 Meat 120.90
120 Picture 34.00
124 Gasoline 11.00
123 Photos;! 71.4?;
122 Picture 93.5
132 Tires;! 19.00,?;
129 Stamps 13.6
129 Fruits{} 17.6
129 Market;! 128.00?;
121 Gasoline;! 13.6?;"""

def parse_checkbook():
check_list = checkbook.split('\n')
check_list_alphanum = [re.sub(r'[^\d\w.\s]', '', line) for line in check_list]
balance = check_list_alphanum[0]
prices = []
for row in check_list_alphanum:
m = re.search(r' (\d.*)', row)
if m is not None:
prices.append(m.group(0))

result = []
check_list_iter = iter(check_list_alphanum)
next(check_list_iter)
prices_iter = iter(prices)
result.append("Original_Balance : {}\n".format(balance))
for row in check_list_iter:
balance = "{0:.2f}".format(float(balance) - float(next(prices_iter)))
new_row = row + " Balance: {}".format(balance)
result.append(new_row+'\n')
prices = [float(p) for p in prices]
result.append("Total expense : {0:.2f}\n".format(sum(prices)))
result.append("Average expense : {0:.2f}\n".format(sum(prices)//len(prices)))
result = ''.join(result)
return result
``````