From d414239704c3cbb7f65fb2f07bb0b270138dc3c7 Mon Sep 17 00:00:00 2001 From: Clod Date: Mon, 6 May 2024 12:42:11 -0300 Subject: [PATCH] tokenizer name="whitespace"/> --- Work/bounce.py | 7 +++++ Work/mortgage.py | 23 ++++++++++++++++ Work/pcost.py | 25 +++++++++++++++++ Work/report.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+) diff --git a/Work/bounce.py b/Work/bounce.py index 3660ddd82..31ffe1a93 100644 --- a/Work/bounce.py +++ b/Work/bounce.py @@ -1,3 +1,10 @@ # bounce.py # # Exercise 1.5 + +height = 100 + +for i in range(1,11): + height = height * 3 / 5 + print(round(height, 4)) + diff --git a/Work/mortgage.py b/Work/mortgage.py index d527314e3..17ad2c02b 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,3 +1,26 @@ # mortgage.py # # Exercise 1.7 + +principal = 500000.0 +rate = 0.05 +payment = 2684.11 +total_paid = 0.0 +months = 0 +extra_payment_start_month = 61 +extra_payment_end_month = 108 +extra_payment = 1000 + +while principal > 0: + if (months < extra_payment_start_month or months > extra_payment_end_month): + principal = principal * (1+rate/12) - payment + total_paid = total_paid + payment + else: + principal = principal * (1+rate/12) - payment - extra_payment + total_paid = total_paid + payment + extra_payment + print(round(total_paid,2),round(principal,2)) + months += 1 + +print('Total paid', round(total_paid,2), '.') +print('Months: ', months) + diff --git a/Work/pcost.py b/Work/pcost.py index e68aa20b4..16c8e29b9 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -1,3 +1,28 @@ # pcost.py # # Exercise 1.27 + +#import os +import sys + +# print ("Current directory: ", os.getcwd()) +def portfolio_cost(filename): + total = 0 + # with open('Work/Data/portfolio.csv', 'rt') as f: + with open(filename, 'rt') as f: + next(f) + for line in f: + row = line.split(',') + try: + total += float(row[1]) * float(row[2]) + except ValueError: + print('Could not parse: ', line) + return total + +if len(sys.argv) == 2: + filename = sys.argv[1] +else: + filename = 'Work/Data/portfolio.csv' + +total = portfolio_cost(filename) +print("Total cost ", round(total, 2)) diff --git a/Work/report.py b/Work/report.py index 47d5da7b1..8b0a68891 100644 --- a/Work/report.py +++ b/Work/report.py @@ -1,3 +1,74 @@ # report.py # # Exercise 2.4 +import csv + +def read_portfolio(filename): + + portfolio = [] + + with open(filename, 'rt') as f: + rows = csv.reader(f) + next(rows) + for row in rows: + holding = (row[0], int(row[1]), float(row[2])) + portfolio.append(holding) + return portfolio + +def read_portfolio_dict(filename): + + portfolio = [] + + with open(filename, 'rt') as f: + rows = csv.reader(f) + headers = next(rows) + for row in rows: + holding = {} + holding[headers[0]] = row[0] + holding[headers[1]] = int(row[1]) + holding[headers[2]] = float(row[2]) + portfolio.append(holding) + return portfolio + +def read_prices(filename): + + prices = {} + + with open(filename, 'rt') as f: + rows = csv.reader(f) + for row in rows: + if (len(row) > 0): + prices[row[0]] = float(row[1]) + return prices + +def make_report(stocks, prices): + report = [] + prices = read_prices('Data/prices.csv') + stocks = read_portfolio_dict('Data/portfolio.csv') + for line in stocks: + #print(line, prices[line['name']], line['price'] - prices[line['name']]) + #tupla = (line['name'], line['shares'], prices[line['name']],line['price'] - prices[line['name']]) + report.append((line['name'], line['shares'], prices[line['name']],line['price'] - prices[line['name']])) + return report + +#print(read_prices('Data/prices.csv')) +stocks = [] +prices = {} + +report = make_report(stocks, prices) + +''' +for r in report: + print('%10s %10d %10.2f %10.2f' % r) +''' + +headers = ('Name', 'Shares', 'Price', 'Change') + +print(f'{headers[0]:>10s}{headers[1]:>11s}{headers[2]:>11s}{headers[3]:>11s}') +#print(f'{"-" * len(headers[0]):>10s}{"-" * len(headers[1]):>11s}{"-" * len(headers[2]):>11s}{"-" * len(headers[3]):>11s}') +print('---------- ---------- ---------- -----------') + +for name, shares, price, change in report: + #print(f'{name:>10s} {shares:>10d} {price:>10.2f} {change:>10.2f}') + print(f'{name:>10s} {shares:>10d} {"$"+str(price):>10s} {change:>10.2f}') +