Skip to content

Commit 264dd2e

Browse files
oslymanrepl.it user
authored andcommitted
Add chapter 1 exercises
1 parent ba57e98 commit 264dd2e

5 files changed

Lines changed: 72 additions & 2 deletions

File tree

.replit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
language = "python3"
2+
run = ""

Work/bounce.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# bounce.py
22
#
33
# Exercise 1.5
4+
5+
for i in range(10):
6+
print(str(i + 1) + ' ' + str(round(60.0 * .6**i, 4)))

Work/mortgage.py

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

Work/pcost.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
# pcost.py
22
#
3-
# Exercise 1.27
3+
# Exercise 1.27, 1.30-3
4+
import csv
5+
import sys
6+
7+
def portfolio_cost(filename):
8+
total_cost = 0
9+
with open(filename, 'rt') as f:
10+
rows = csv.reader(f)
11+
headers = next(rows)
12+
for r in rows:
13+
try:
14+
total_cost += float(r[1]) * float(r[2])
15+
except ValueError:
16+
print("WARN: the csv file is missing a value, skipping this row")
17+
return total_cost
18+
19+
if len(sys.argv) == 2:
20+
filename = sys.argv[1]
21+
else:
22+
filename = 'Data/portfolio.csv'
23+
cost = portfolio_cost(filename)
24+
print(f'Total cost {round(cost, 2):0.2f}')

Work/sears.py

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

0 commit comments

Comments
 (0)