From db57de7117010be18c5f2f5b7795edbd8629e3aa Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 00:16:25 -0400 Subject: [PATCH 01/19] ex: 1.5 --- Work/bounce.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Work/bounce.py b/Work/bounce.py index 3660ddd82..190d5d80b 100644 --- a/Work/bounce.py +++ b/Work/bounce.py @@ -1,3 +1,8 @@ # bounce.py # # Exercise 1.5 +height = 100 + +for i in range(10): + height = height * 3 / 5 + print(i+1, round(height,4)) \ No newline at end of file From 2af8b37bf7239145695ea1842597c350d46eef86 Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 00:17:50 -0400 Subject: [PATCH 02/19] ex: 1.6 --- Work/sears.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Work/sears.py diff --git a/Work/sears.py b/Work/sears.py new file mode 100644 index 000000000..8add59254 --- /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) \ No newline at end of file From 64a6d9139a05fbc6ea7adb5a43ca1a26228bfe5c Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 00:23:36 -0400 Subject: [PATCH 03/19] ex: 1.7 --- Work/mortgage.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Work/mortgage.py b/Work/mortgage.py index d527314e3..3ec44983e 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,3 +1,12 @@ # mortgage.py -# -# Exercise 1.7 + +principal = 500000.0 +rate = 0.05 +payment = 2684.11 +total_paid = 0.0 + +while principal > 0: + principal = principal * (1+rate/12) - payment + total_paid = total_paid + payment + +print('Total paid', total_paid) \ No newline at end of file From 2841a2a91e1b3335da073a4852cbfc79fd17562d Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 00:23:54 -0400 Subject: [PATCH 04/19] ex: 1.8 --- Work/mortgage.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Work/mortgage.py b/Work/mortgage.py index 3ec44983e..d3b1e2d45 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -4,9 +4,15 @@ rate = 0.05 payment = 2684.11 total_paid = 0.0 +month = 0 while principal > 0: - principal = principal * (1+rate/12) - payment + if month < 12: + month_payment = payment + 1000 + else: + month_payment = payment + month = month + 1 + principal = principal * (1+rate/12) - month_payment total_paid = total_paid + payment -print('Total paid', total_paid) \ No newline at end of file +print('Total paid', total_paid, 'Months required', month) \ No newline at end of file From 1f3570968f3f8e047347074ff24495c216b53b9c Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 00:26:44 -0400 Subject: [PATCH 05/19] ex: 1.9 --- Work/mortgage.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Work/mortgage.py b/Work/mortgage.py index d3b1e2d45..17bce7e5d 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -4,11 +4,14 @@ rate = 0.05 payment = 2684.11 total_paid = 0.0 +extra_payment_start_month = 60 +extra_payment_end_month = 108 +extra_payment = 1000 month = 0 while principal > 0: - if month < 12: - month_payment = payment + 1000 + if month >= extra_payment_start_month and month <= extra_payment_end_month: + month_payment = payment + extra_payment else: month_payment = payment month = month + 1 From af5ca6b737fc6aef0de6221bfe2563f35ae36965 Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 00:28:25 -0400 Subject: [PATCH 06/19] ex: 1.10 --- Work/mortgage.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Work/mortgage.py b/Work/mortgage.py index 17bce7e5d..2245a506e 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -17,5 +17,6 @@ month = month + 1 principal = principal * (1+rate/12) - month_payment total_paid = total_paid + payment + print(month, round(total_paid, 2), round(principal, 2)) -print('Total paid', total_paid, 'Months required', month) \ No newline at end of file +print('Total paid', round(total_paid, 2), '\nMonths', month) \ No newline at end of file From bdb134d14a3465efd61013e68e216d4811d8055f Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 00:58:54 -0400 Subject: [PATCH 07/19] ex: 1.10 --- Work/mortgage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Work/mortgage.py b/Work/mortgage.py index 2245a506e..a50e7ce69 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -10,11 +10,11 @@ month = 0 while principal > 0: + month = month + 1 if month >= extra_payment_start_month and month <= extra_payment_end_month: month_payment = payment + extra_payment else: month_payment = payment - month = month + 1 principal = principal * (1+rate/12) - month_payment total_paid = total_paid + payment print(month, round(total_paid, 2), round(principal, 2)) From bb6c2b439e57af41e5421a900a9ee7ad66e75a09 Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 01:00:53 -0400 Subject: [PATCH 08/19] ex: 1.10 - bugfix --- Work/mortgage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Work/mortgage.py b/Work/mortgage.py index a50e7ce69..8a2e74291 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -16,7 +16,7 @@ else: month_payment = payment principal = principal * (1+rate/12) - month_payment - total_paid = total_paid + payment + total_paid = total_paid + month_payment print(month, round(total_paid, 2), round(principal, 2)) print('Total paid', round(total_paid, 2), '\nMonths', month) \ No newline at end of file From 2b8dc674e321b155123c35aff7f3ddd83b0afd3c Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 01:03:07 -0400 Subject: [PATCH 09/19] ex: 1.11 --- Work/mortgage.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Work/mortgage.py b/Work/mortgage.py index 8a2e74291..8fae78268 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -15,6 +15,7 @@ month_payment = payment + extra_payment else: month_payment = payment + month_payment = min(month_payment, principal * (1 + rate / 12)) principal = principal * (1+rate/12) - month_payment total_paid = total_paid + month_payment print(month, round(total_paid, 2), round(principal, 2)) From d9ca0d4e638bb570628022814dfe59564bde467f Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 08:45:14 -0400 Subject: [PATCH 10/19] ex: 1.17 --- Work/mortgage.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Work/mortgage.py b/Work/mortgage.py index 8fae78268..5387f99e4 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -18,6 +18,6 @@ month_payment = min(month_payment, principal * (1 + rate / 12)) principal = principal * (1+rate/12) - month_payment total_paid = total_paid + month_payment - print(month, round(total_paid, 2), round(principal, 2)) + print(f"{month:10d} {total_paid:10.2f} {principal:10.2f}") -print('Total paid', round(total_paid, 2), '\nMonths', month) \ No newline at end of file +print(f'Total paid {total_paid:10.2f}\nMonths {month}') \ No newline at end of file From 37175139e69026b3cad9e961862c4e5d448baec9 Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 09:14:46 -0400 Subject: [PATCH 11/19] ex: 1.27 --- Work/pcost.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Work/pcost.py b/Work/pcost.py index e68aa20b4..f1371ddb9 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -1,3 +1,12 @@ # pcost.py # # Exercise 1.27 + +cost = 0 +with open("./Data/portfolio.csv", "rt") as f: + next(f) # skip headers + for line in f: + ticker, nshares, share_cost = line.split(",") + cost += int(nshares) * float(share_cost) + +print(f"Total cost: {cost}") \ No newline at end of file From 12f086518ed4cecc619ccfba352fdfb2a3a478e0 Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 09:24:11 -0400 Subject: [PATCH 12/19] ex: 1.30 --- Work/pcost.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Work/pcost.py b/Work/pcost.py index f1371ddb9..2c05be64e 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -2,11 +2,15 @@ # # Exercise 1.27 -cost = 0 -with open("./Data/portfolio.csv", "rt") as f: - next(f) # skip headers - for line in f: - ticker, nshares, share_cost = line.split(",") - cost += int(nshares) * float(share_cost) +def portfolio_cost(filename): + cost = 0 + with open(filename, "rt") as f: + next(f) # skip headers + for line in f: + ticker, nshares, share_cost = line.split(",") + cost += int(nshares) * float(share_cost) + return cost + +cost = portfolio_cost("Data/portfolio.csv") print(f"Total cost: {cost}") \ No newline at end of file From 866dddee963e80833d479ffafa1dee2c28093374 Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 09:26:04 -0400 Subject: [PATCH 13/19] ex: 1.31 --- Work/pcost.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Work/pcost.py b/Work/pcost.py index 2c05be64e..dbddca77c 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -8,7 +8,10 @@ def portfolio_cost(filename): next(f) # skip headers for line in f: ticker, nshares, share_cost = line.split(",") - cost += int(nshares) * float(share_cost) + try: + cost += int(nshares) * float(share_cost) + except ValueError: + print(f"Invalid entries found nshares: {nshares}, share_cost: {share_cost}") return cost From 8a0b13c003b41963f0e296b0663a41f204e75885 Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 09:28:20 -0400 Subject: [PATCH 14/19] ex: 1.32 --- Work/pcost.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Work/pcost.py b/Work/pcost.py index dbddca77c..0caa680c6 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -1,13 +1,15 @@ # pcost.py # # Exercise 1.27 +import csv def portfolio_cost(filename): cost = 0 with open(filename, "rt") as f: - next(f) # skip headers - for line in f: - ticker, nshares, share_cost = line.split(",") + reader = csv.reader(f) + headers = next(reader) # skip headers + for row in reader: + ticker, nshares, share_cost = row try: cost += int(nshares) * float(share_cost) except ValueError: From f90572948b925fe2c7ddc9e87fb435db01bc3841 Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 09:28:49 -0400 Subject: [PATCH 15/19] ex: 1.32 --- Work/pcost.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Work/pcost.py b/Work/pcost.py index 0caa680c6..b478a4ff3 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -8,8 +8,7 @@ def portfolio_cost(filename): with open(filename, "rt") as f: reader = csv.reader(f) headers = next(reader) # skip headers - for row in reader: - ticker, nshares, share_cost = row + for ticker, nshares, share_cost in reader: try: cost += int(nshares) * float(share_cost) except ValueError: From 697009879de0fca9ec0e534a17c845f2ab754ec4 Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 09:32:16 -0400 Subject: [PATCH 16/19] ex: 1.33 --- Work/pcost.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Work/pcost.py b/Work/pcost.py index b478a4ff3..abc2caa27 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -2,6 +2,7 @@ # # Exercise 1.27 import csv +import sys def portfolio_cost(filename): cost = 0 @@ -16,5 +17,10 @@ def portfolio_cost(filename): return cost -cost = portfolio_cost("Data/portfolio.csv") +if len(sys.argv) == 2: + filename = sys.argv[1] +else: + filename = "Data/portfolio.csv" + +cost = portfolio_cost(filename) print(f"Total cost: {cost}") \ No newline at end of file From fe94c0092cf733ca97d4aa33f4112611f116fab5 Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 09:44:09 -0400 Subject: [PATCH 17/19] ex: 2.4 --- Work/report.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Work/report.py b/Work/report.py index 47d5da7b1..3c3b47379 100644 --- a/Work/report.py +++ b/Work/report.py @@ -1,3 +1,18 @@ # report.py # # Exercise 2.4 +import csv + +def read_portfolio(filename): + + portfolio = [] + + with open(filename, 'rt') as f: + reader = csv.reader(f) + headers = next(reader) + for ticker, nshares, share_price in reader: + nshares = int(nshares) + share_price = float(share_price) + portfolio.append((ticker, nshares, share_price)) + + return portfolio \ No newline at end of file From 97f290c50503f3de91e33c1e36e13b87e17400bc Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 09:46:00 -0400 Subject: [PATCH 18/19] ex: 2.5 --- Work/report.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Work/report.py b/Work/report.py index 3c3b47379..403c825b0 100644 --- a/Work/report.py +++ b/Work/report.py @@ -13,6 +13,6 @@ def read_portfolio(filename): for ticker, nshares, share_price in reader: nshares = int(nshares) share_price = float(share_price) - portfolio.append((ticker, nshares, share_price)) + portfolio.append({"name": ticker, "shares": nshares, "price": share_price}) return portfolio \ No newline at end of file From 9e40ea272c3137a28575476efc0f9227368c4bbe Mon Sep 17 00:00:00 2001 From: asishm Date: Sat, 30 May 2020 09:56:15 -0400 Subject: [PATCH 19/19] ex: 2.7 --- Work/report.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Work/report.py b/Work/report.py index 403c825b0..a886a7ef3 100644 --- a/Work/report.py +++ b/Work/report.py @@ -15,4 +15,31 @@ def read_portfolio(filename): share_price = float(share_price) portfolio.append({"name": ticker, "shares": nshares, "price": share_price}) - return portfolio \ No newline at end of file + return portfolio + +def read_prices(filename): + + prices = {} + with open(filename, 'rt') as f: + reader = csv.reader(f) + for row in reader: + try: + ticker, price = row + price = float(price) + prices[ticker] = price + except ValueError as e: + print(f"Skipping row: {row}, error: {e}") + return prices + +portfolio = read_portfolio("Data/portfolio.csv") +prices = read_prices("Data/prices.csv") + +portfolio_cost = 0 +portfolio_value = 0 +for stock in portfolio: + portfolio_cost += stock['shares'] * stock['price'] + portfolio_value += stock['shares'] * prices[stock['name']] + +print(f"Total cost: {portfolio_cost}") +print(f"Total value: {portfolio_value}") +print(f"Net gain/loss: {portfolio_value - portfolio_cost}") \ No newline at end of file