From 56bcea8b75df9f0bacc6c1bb0a6323d8c81e27d5 Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Fri, 26 Jun 2020 15:32:21 -0400 Subject: [PATCH 1/4] finished exercise 1.5 --- Work/bounce.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Work/bounce.py b/Work/bounce.py index 3660ddd82..b609a0be0 100644 --- a/Work/bounce.py +++ b/Work/bounce.py @@ -1,3 +1,13 @@ # bounce.py # # Exercise 1.5 + +current_height = 100 +bounce_factor = 3 / 5 +bounce_number = 1 + + +while bounce_number <= 10: + current_height = current_height * bounce_factor + print(bounce_number, round(current_height, 4)) + bounce_number += 1 From 771d346580eae8c379c8d389a7d9bffbbe1cb301 Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Fri, 26 Jun 2020 20:38:33 -0400 Subject: [PATCH 2/4] finished exercise 1.11 --- Work/mortgage.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Work/mortgage.py b/Work/mortgage.py index d527314e3..35a1030f1 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,3 +1,29 @@ # mortgage.py # # Exercise 1.7 + +principal = 500000.0 +rate = 0.05 +payment = 2684.11 +extra_payment = 1000 +extra_payment_start_month = 61 +extra_payment_end_month = 108 +total_paid = 0.0 +month = 1 + +while principal > 0: + if month <= extra_payment_end_month and month >= extra_payment_start_month: + monthly_payment = payment + extra_payment + else: + monthly_payment = payment + principal = principal * (1 + rate / 12) + if principal >= monthly_payment: + principal = principal - monthly_payment + else: + monthly_payment = principal + principal = 0 + total_paid = total_paid + monthly_payment + print(month, total_paid, principal) + month += 1 + +print("Total paid", total_paid) From 9947cc02fcd0c736c3249bd47822f6a46c09ce66 Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Fri, 26 Jun 2020 21:12:12 -0400 Subject: [PATCH 3/4] finished exercise 1.28 --- Work/pcost.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Work/pcost.py b/Work/pcost.py index e68aa20b4..14f4b5879 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -1,3 +1,16 @@ # pcost.py # # Exercise 1.27 + +import os +import gzip + +total_cost = 0 + +with gzip.open("Data/portfolio.csv.gz", "rt") as f: + next(f) + for l in f: + [shares, price] = l.split(",")[1:] + total_cost = total_cost + int(shares) * float(price) + +print(total_cost) From 9ec4c7b8ce07d482b2a7a652b9843c4334465f04 Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Fri, 26 Jun 2020 21:31:08 -0400 Subject: [PATCH 4/4] exercise 1.32 complete --- Work/pcost.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Work/pcost.py b/Work/pcost.py index 14f4b5879..65f90c298 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -3,14 +3,23 @@ # Exercise 1.27 import os -import gzip +import csv -total_cost = 0 -with gzip.open("Data/portfolio.csv.gz", "rt") as f: - next(f) - for l in f: - [shares, price] = l.split(",")[1:] - total_cost = total_cost + int(shares) * float(price) +def portfolio_cost(filename): + "returns total cost of the portfolio from filename" + total_cost = 0 + with open(filename, "rt") as f: + rows = csv.reader(f) + next(rows) + for l in rows: + try: + [shares, price] = l[1:] + total_cost = total_cost + int(shares) * float(price) + except: + print("Could not parse:", l) + return total_cost -print(total_cost) + +total_cost = portfolio_cost("Data/missing.csv") +print("Total cost:", total_cost)