diff --git a/Work/bounce.py b/Work/bounce.py index 3660ddd82..572f6d759 100644 --- a/Work/bounce.py +++ b/Work/bounce.py @@ -1,3 +1,12 @@ # bounce.py # # Exercise 1.5 + +height = 100 +ratio = 3/5 +bounces = 1 + +while height > 1: + height = height * ratio + print(bounces, round(height, 4)) + bounces = bounces + 1 diff --git a/Work/mortgage.py b/Work/mortgage.py index d527314e3..f711d2709 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,3 +1,41 @@ # mortgage.py # # Exercise 1.7 +# mortgage.py + +principal = 500000.0 +rate = 0.05 +original_payment = 2684.11 +total_paid = 0.0 +month = 0 + +extra_payment_start_month = 61 +extra_payment_end_month = 108 +extra_payment = 1000 + +while principal > 0: + # larger payment for starting 12 months + # if month < 12: + # payment = original_payment + 1000 + # else: + # payment = original_payment + payment = original_payment + + # start of month payment + if month % 12 == 0: + payment = payment + extra_payment_start_month + # end of month payment + if month % 12 == 11: + payment = payment + extra_payment_end_month + + # extra payment for 4 years after first 5 years + if month >= 12 * 5 and month < 12 * 9: + payment = payment + extra_payment + + month = month + 1 + principal = principal * (1+rate/12) - payment + total_paid = total_paid + payment + + print(f'In month {month} total amount paid is ${total_paid}, left principal is ${principal}') + +print('Total paid', total_paid, 'over', month) diff --git a/Work/pcost.py b/Work/pcost.py index e68aa20b4..145dfa93d 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -1,3 +1,26 @@ # pcost.py # # Exercise 1.27 +import sys + +def portfolio_cost(filename): + with open(filename, 'rt') as f: + headers = next(f) + + total = 0 + for line in f: + name, shares, price = line.split(',') + try: + total = total + int(shares.strip()) * float(price.strip()) + except ValueError as e: + print('Encountered unproccessable values!') + + return total + +if len(sys.argv) == 2: + filename = sys.argv[1] +else: + filename = 'Data/portfolio.csv' + +cost = portfolio_cost(filename) +print('Total cost:', cost) diff --git a/Work/report.py b/Work/report.py index 47d5da7b1..fdd404269 100644 --- a/Work/report.py +++ b/Work/report.py @@ -1,3 +1,52 @@ # report.py # # Exercise 2.4 + +import csv + +def read_portfolio(filename): + """Open portfolio and read into structured data + + :filename: name of portfolio file + :returns: python list of shares and their information + + """ + portfolio = [] + + with open(filename, 'rt') as f: + rows = csv.reader(f) + headers = next(rows) + for row in rows: + holding = { + 'name': row[0], + 'shares': int(row[1]), + 'price': float(row[2]) + } + + portfolio.append(holding) + + return portfolio + +def read_prices(filename): + """Open prices file and read into structured data + + :filename: name of prices file + :returns: python list of prices for a share + + """ + prices = [] + + with open(filename, 'rt') as f: + rows = csv.reader(f) + for row in rows: + try: + share = { + 'name': row[0], + 'price': float(row[1]) + } + prices.append(share) + except IndexError as e: + print("Error indexing incoming data") + continue + + return prices diff --git a/Work/sears.py b/Work/sears.py new file mode 100644 index 000000000..df968fff7 --- /dev/null +++ b/Work/sears.py @@ -0,0 +1,15 @@ +# sears.py + +bill_thickness = 0.11 * 0.001 # Meters (0.11 mm) +sears_height = 442 # Height (meters) +num_bills = 1 +day = 1 + +while num_bills * bill_thickness < sears_height: + print(day, num_bills, num_bills * bill_thickness) + day = day + 1 + num_bills = num_bills * 2 + +print('Number of days', day) +print('Number of bills', num_bills) +print('Final height', num_bills * bill_thickness)