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 diff --git a/Work/mortgage.py b/Work/mortgage.py index d527314e3..5387f99e4 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,3 +1,23 @@ # mortgage.py -# -# Exercise 1.7 + +principal = 500000.0 +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: + 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_payment = min(month_payment, principal * (1 + rate / 12)) + principal = principal * (1+rate/12) - month_payment + total_paid = total_paid + month_payment + print(f"{month:10d} {total_paid:10.2f} {principal:10.2f}") + +print(f'Total paid {total_paid:10.2f}\nMonths {month}') \ No newline at end of file diff --git a/Work/pcost.py b/Work/pcost.py index e68aa20b4..abc2caa27 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -1,3 +1,26 @@ # pcost.py # # Exercise 1.27 +import csv +import sys + +def portfolio_cost(filename): + cost = 0 + with open(filename, "rt") as f: + reader = csv.reader(f) + headers = next(reader) # skip headers + for ticker, nshares, share_cost in reader: + try: + cost += int(nshares) * float(share_cost) + except ValueError: + print(f"Invalid entries found nshares: {nshares}, share_cost: {share_cost}") + + return cost + +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 diff --git a/Work/report.py b/Work/report.py index 47d5da7b1..a886a7ef3 100644 --- a/Work/report.py +++ b/Work/report.py @@ -1,3 +1,45 @@ # 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({"name": ticker, "shares": nshares, "price": share_price}) + + 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 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