diff --git a/Work/bounce.py b/Work/bounce.py index 3660ddd82..4bb8e296b 100644 --- a/Work/bounce.py +++ b/Work/bounce.py @@ -1,3 +1,14 @@ # bounce.py # # Exercise 1.5 +# A rubber ball is dropped from a height of 100 meters and each time it hits the ground, +# it bounces back up to 3/5 the height it fell. +# Write a program bounce.py that prints a table showing the height of the first 10 bounces. +height = 100 +constant = 3/5 +n = 1 + +while n <= 10: + height *= constant + print(round(height, 4)) + n += 1 diff --git a/Work/mortgage.py b/Work/mortgage.py index d527314e3..c30f1d1c5 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,3 +1,20 @@ # mortgage.py # # Exercise 1.7 +principal = 500000.0 +rate = 0.05 +payment = 2684.11 +total_paid = 0.0 +extra_payment = 1000 +month = 1 +while principal > 0: + if month <= 12: + special_payment = payment + extra_payment + else: + special_payment = payment + principal = principal * (1+rate/12) - special_payment + total_paid = total_paid + special_payment + month += 1 + +print('Total paid', total_paid) +print('Total month', month-1) diff --git a/Work/mortgage19.py b/Work/mortgage19.py new file mode 100644 index 000000000..ce9657973 --- /dev/null +++ b/Work/mortgage19.py @@ -0,0 +1,24 @@ +principal = 500000.0 +rate = 0.05 +payment = 2684.11 +total_paid = 0.0 +extra_payment = 1000 +month = 1 +extra_payment_start_month = 60 +extra_payment_end_month = 108 +extra_payment = 1000 + +while principal > 0: + if extra_payment_start_month <= month <= extra_payment_end_month: + special_payment = payment + extra_payment + else: + special_payment = payment + if special_payment > principal: + special_payment = principal * (1+rate/12) + principal = principal * (1+rate/12) - special_payment + total_paid = total_paid + special_payment + print(month, total_paid, round(principal, 2)) + month += 1 + +print('Total paid', total_paid) +print('Total month', month-1) diff --git a/Work/notes.md b/Work/notes.md new file mode 100644 index 000000000..199dd7030 --- /dev/null +++ b/Work/notes.md @@ -0,0 +1,47 @@ +## Integers (int) + +x + y Add +x - y Subtract +x * y Multiply +x / y Divide (produces a float) +x // y Floor Divide (produces an integer) +x % y Modulo (remainder) +x ** y Power +x << n Bit shift left +x >> n Bit shift right +x & y Bit-wise AND +x | y Bit-wise OR +x ^ y Bit-wise XOR +~x Bit-wise NOT +abs(x) Absolute value + +## Float + +x + y Add +x - y Subtract +x * y Multiply +x / y Divide +x // y Floor Divide +x % y Modulo +x ** y Power +abs(x) Absolute Value + +## Strings + +### Escape codes + +'\n' Line feed +'\r' Carriage return +'\t' Tab +'\'' Literal single quote +'\"' Literal double quote +'\\' Literal backslash + +### String Representation + +a = '\xf1' # a = 'ñ' +b = '\u2200' # b = '∀' +c = '\U0001D122' # c = '𝄢' +d = '\N{FOR ALL}' # d = '∀' + +https://unicode.org/charts/ diff --git a/Work/pcost.py b/Work/pcost.py index e68aa20b4..99df7e545 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -1,3 +1,21 @@ # pcost.py # # Exercise 1.27 +import csv + + +def portfolio_cost(filename): + f = open(filename, "rt") + rows = csv.reader(f) + headers = next(rows) + total = 0.0 + for rowno, row in enumerate(rows): + try: + record = dict(zip(headers, row)) + shares = int(record['shares']) + price = float(record['price']) + total += shares*price + except ValueError: + print(f'Row {rowno}: Bad row: {row}') + f.close() + return total diff --git a/Work/pcost_function.py b/Work/pcost_function.py new file mode 100644 index 000000000..9de9794b1 --- /dev/null +++ b/Work/pcost_function.py @@ -0,0 +1,19 @@ +# pcost.py +# +# Exercise 1.27 + +import csv + + +def portfolio_cost(filename): + try: + f = open(filename, "rt") + rows = csv.reader(f) + next(rows) + total = 0 + for row in rows: + total += int(row[1])*float(row[2]) + f.close() + return total + except Exception: + print("Couldn't parse", Exception) diff --git a/Work/report.py b/Work/report.py index 47d5da7b1..18439a914 100644 --- a/Work/report.py +++ b/Work/report.py @@ -1,3 +1,47 @@ # report.py # # Exercise 2.4 + +import csv + + +def read_portfolio(filename): + try: + f = open(filename, 'rt') + rows = csv.reader(f) + headers = next(rows) + portfolio = [] + + for row in rows: + symbol = (row[0], int(row[1]), float(row[2])) + portfolio.append(symbol) + except: + print("Can't read file", filename) + return portfolio + + +def read_prices(filename): + try: + f = open(filename, 'rt') + rows = csv.reader(f) + prices = [] + for row in rows: + if len(row) > 0: + price = (row[0], float(row[1])) + prices.append(price) + except: + print("Can't read price file", filename) + return prices + + +def make_report(portfolio, prices): + report = [] + for price in prices: + print(price) + for share in portfolio: + print(share) + if price[0] == share[0]: + print(price[0]) + change = share[2] - price[1] + report.append((share[0], share[1], share[2], change)) + return report diff --git a/Work/report_dict.py b/Work/report_dict.py new file mode 100644 index 000000000..3fa34fc8c --- /dev/null +++ b/Work/report_dict.py @@ -0,0 +1,36 @@ + +def read_portfolio(filename): + try: + f = open(filename, 'rt') + rows = csv.reader(f) + headers = next(rows) + portfolio = [] + + for row in rows: + symbol = {headers[0]: row[0], headers[1]: int(row[1]), headers[2]: float(row[2])} + portfolio.append(symbol) + except: + print("Can't read file", filename) + return portfolio + + +def read_prices(filename): + try: + f = open(filename, 'rt') + rows = csv.reader(f) + prices = [] + for row in rows: + if len(row) > 0: + price = {'name': row[0], 'price': float(row[1])} + prices.append(price) + except: + print("Can't read price file", filename) + return prices + + +def make_report(portfolio, prices): + for price in prices: + for share in portfolio: + if price['name'] == share['name']: + share['change'] = share['price'] - price['price'] + return portfolio diff --git a/Work/sears.py b/Work/sears.py new file mode 100644 index 000000000..11bfa60e0 --- /dev/null +++ b/Work/sears.py @@ -0,0 +1,17 @@ +# One morning, you go out and place a dollar bill on the sidewalk by the Sears tower in Chicago. +# Each day thereafter, you go out double the number of bills. How long does it take for the stack +# of bills to exceed the height of the tower? + +bill_thickness = 0.11 * 0.001 # Meters (0.11 mm) +sears_height = 442 +num_bills = 1 +day = 1 + +while num_bills*bill_thickness < sears_height: + print(day, num_bills, num_bills*bill_thickness) + day += 1 + num_bills *= 2 + +print('Number of days', day) +print('Number of bills', num_bills) +print('Final height', num_bills * bill_thickness)