Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5ae276e

Browse files
committed
finish chapter 01 exercises
1 parent 785b986 commit 5ae276e

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

Work/bounce.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
# bounce.py
22
#
33
# Exercise 1.5
4+
5+
height = 100
6+
bounce_efficiency = 3/5
7+
8+
for bounce in range(1, 11):
9+
height = height * bounce_efficiency
10+
print(bounce, round(height, 4))

Work/mortgage.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
11
# mortgage.py
22
#
33
# Exercise 1.7
4+
5+
principal = 500000.0
6+
rate = 0.05
7+
base_payment = 2684.11
8+
total_paid = 0.0
9+
month = 0
10+
11+
# Extra payments
12+
extra_payment_start_month = 60
13+
extra_payment_end_month = 108
14+
extra_payment = 1000
15+
16+
while principal > 0:
17+
if month in range(extra_payment_start_month, extra_payment_end_month):
18+
payment = base_payment + extra_payment
19+
else:
20+
payment = base_payment
21+
principal = principal * (1 + rate/12) - payment
22+
if principal < 0:
23+
payment += principal
24+
principal = 0
25+
total_paid = total_paid + payment
26+
month += 1
27+
print(f'{month:>3d} {total_paid:>10.2f} {principal:>10.2f}')
28+
29+
print('Total paid', round(total_paid, 2))
30+
print('Months', month)

Work/pcost.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
11
# pcost.py
22
#
33
# Exercise 1.27
4+
5+
import csv
6+
import sys
7+
8+
9+
def portfolio_cost(filename):
10+
total = 0
11+
12+
with open(filename, 'rt') as f:
13+
portfolio = csv.reader(f)
14+
headers = next(portfolio)
15+
for row in portfolio:
16+
try:
17+
symbol, shares, price = row
18+
total += int(shares) * float(price)
19+
except ValueError:
20+
print(f'Error parsing row: {row} ... Skipping...')
21+
22+
return total
23+
24+
25+
if len(sys.argv) == 2:
26+
filename = sys.argv[1]
27+
else:
28+
filename = 'Data/portfolio.csv'
29+
30+
cost = portfolio_cost(filename)
31+
print(f'Total cost: ${cost:>7.2f}')

Work/sears.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# sears.py
2+
bill_thickness = 0.11 * 0.001 # Meters (0.11 mm)
3+
sears_height = 442 # Height (meters)
4+
num_bills = 1
5+
day = 1
6+
7+
while num_bills * bill_thickness < sears_height:
8+
print(day, num_bills, num_bills * bill_thickness)
9+
day = day + 1
10+
num_bills = num_bills * 2
11+
12+
print('Number of days', day)
13+
print('Number of bills', num_bills)
14+
print('Final height', num_bills * bill_thickness)

0 commit comments

Comments
 (0)