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
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Python: