Skip to content

Commit e5e3c95

Browse files
committed
Chapter 9
1 parent cfa63ad commit e5e3c95

File tree

332 files changed

+64889
-61331
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

332 files changed

+64889
-61331
lines changed

Diff for: 8.1.py

+10-24
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,14 @@
1-
# Program to compute the nth Fibonnaci number
2-
31
def main():
4-
print("This program computes the nth Fibonacci number")
5-
n1 = 0
6-
n2 = 1
7-
count = 0
8-
n = int(input("Enter the nth number of the Fibonacci sequence: "))
9-
fibonacci = n - 1 + n - 2
2+
n = int(input("Enter a number of Fibonnaci sequence\
3+
you'd like to know: "))
4+
5+
current = 1
6+
previous = 1
107

11-
12-
13-
while True:
14-
if n <= 0:
15-
n = int(input("Enter a postivie number: "))
16-
elif n == 1:
17-
print("0")
18-
break
19-
else:
20-
while count < n:
21-
print(n1)
22-
num = n1 + n2
23-
n1 = n2
24-
n2 = num
25-
count = count + 1
26-
break
8+
for i in range(n - 2):
9+
current += previous
10+
previous = current
11+
12+
print(current)
2713

2814
main()

Diff for: 8.2.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# Program to compute the windchill index
2-
import math
3-
41
def main():
5-
temperature = - 20
6-
velocity = 0
7-
formula = 35.74 + 0.6215 * temperature - 35.75 * \
8-
(math.pow(velocity, 0.16))\
9-
+ 0.4275 * t *(math.pow(velocity, 0.16))
102

11-
print(formula)
3+
for velocity in range(0, 50, 5):
4+
print("\t")
5+
for temperature in range(-20, 60, 10):
6+
index = 35.74 + 0.6215 * temperature - 35.75 * (velocity *0.16 * 0.16) + 0.4275 * (temperature* 0.16 * 0.16)
7+
print(str(round(velocity, 2)) + "\t", end='')
8+
print(str(round(index, 2)), end='')
9+
10+
1211

12+
1313
main()

Diff for: 8.3 (exercise).py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def main():
2+
3+
investment = 10
4+
interest_rate = float(input("Enter annualized interest rate: "))
5+
result = 0
6+
condition = investment * 2
7+
while(investment < condition):
8+
9+
result += 1
10+
investment = investment * (1 + interest_rate)
11+
12+
print("You'll have to wait " + str(result) + " years.")
13+
14+
return result
15+
main()

Diff for: 8.3.1.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def fibo():
2+
n = int(input("Enter a positive integer: "))
3+
total = 0
4+
i = 0
5+
while i <= n:
6+
total += i
7+
i += 1
8+
print(total)
9+
10+
fibo()

Diff for: 8.3.2.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def fibo():
2+
n = int(input("Enter a positive integer: "))
3+
total = 0
4+
i = 0
5+
while i <= n:
6+
if i % 2 != 0:
7+
total += i
8+
i += 1
9+
print(total + 2 * n - 1)
10+
11+
fibo()

Diff for: 8.3.3.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def fibo():
2+
lst = []
3+
i = 0
4+
total = 0
5+
while 999 not in lst:
6+
n = int(input("The loop stops after you enter 999: "))
7+
lst.append(n)
8+
print(lst)
9+
10+
while lst[i] != 999:
11+
total += lst[i]
12+
i += 1
13+
print(total)
14+
15+
fibo()

Diff for: 8.3.4.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def fibo():
2+
n = int(input("Enter a whole number: "))
3+
count = 1
4+
while (n // 2) != 1:
5+
n //= 2
6+
count += 1
7+
8+
print(count)
9+
fibo()

Diff for: 8.3.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def main():
2+
print("Calculate the number of years it takes to double an investment")
3+
interest = float(input("Enter annualized interest rate: "))
4+
investment = 1
5+
6+
while
7+
8+
9+
main()

Diff for: 8.4.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def main():
2+
x = int(input("Enter a natural positive number: "))
3+
4+
while x > 1:
5+
if x % 2 == 0:
6+
x /= 2
7+
print(x)
8+
else:
9+
x = 3 * x + 1
10+
print(x)
11+
12+
13+
main()

Diff for: 8.5.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def is_prime(a):
2+
3+
return all(a % i for i in range(2, a))
4+
5+
6+
def main():
7+
n = int(input("Enter a natural positive number to check\
8+
f it's prime: "))
9+
if (is_prime(n)):
10+
print("The number " + str(n) + " is prime.")
11+
else:
12+
print("The number " + str(n) + " isn't prime.")
13+
14+
main()

Diff for: Chapter01.html

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2+
<html>
3+
<head>
4+
<title>Chapter 1</title>
5+
6+
<meta http-equiv="content-type"
7+
content="text/html; charset=ISO-8859-1">
8+
9+
<link rel=stylesheet type="text/css" href="style.css"/>
10+
</head>
11+
<body>
12+
13+
<h1>Chapter 1</h1>
14+
15+
<h2>True/False</h2>
16+
17+
<ol>
18+
<li>False</li>
19+
<li>True</li>
20+
<li>False</li>
21+
<li>True</li>
22+
<li>False</li>
23+
<li>True</li>
24+
<li>False</li>
25+
<li>True</li>
26+
<li>False</li>
27+
<li>False (it can be computed, but the result may not be useful)</li>
28+
29+
</ol>
30+
31+
<h2>Multiple Choice</h2>
32+
33+
<ol>
34+
<li>b</li>
35+
<li>d</li>
36+
<li>d</li>
37+
<li>a</li>
38+
<li>b</li>
39+
<li>b</li>
40+
<li>c</li>
41+
<li>b</li>
42+
<li>a</li>
43+
<li>d</li>
44+
45+
</ol>
46+
47+
<h2>Discussion</h2>
48+
49+
<ol>
50+
<li>Actual answers will vary and should include more detail. Here are
51+
the outlines of some answers.
52+
<ol type="a">
53+
<li>&nbsp;Hardware is the physical components while software is the
54+
programs.</li>
55+
<li>Both may be thought of as a sequence of steps to solve a problem.
56+
A program is a specific implementation of some algorithm that can be executed
57+
by a computer.</li>
58+
<li>Programming languages used to write instuctions for computers
59+
are precise. Natural languages used for human communication are ambiguous.</li>
60+
<li>High-level languages are intended to be easier for humans and
61+
must be translated into the machine language that the hardware of a particular
62+
computer understands.</li>
63+
<li>A compiler provides a one-shot translation, while an interpreter
64+
simulates a machine that understands a high-level language.</li>
65+
<li>Syntax is form and semantics is meaning.</li>
66+
67+
</ol>
68+
</li>
69+
<li>See discussion of Figure 1.1 in the text (Pages 5 and 6).</li>
70+
<li>Students will have varying degrees of success with this problem.
71+
Strictly speaking it is not really well specified, as we don't have an agreed
72+
upon vocabulary for expressing the steps of an algorithm. This question can
73+
be used to illustrate some of the difficulties of using natural language to
74+
describe processes in detail.</li>
75+
<li>If a float is used to represent a quantity in a chaotic function,
76+
then a very small rounding error can lead to results that are utlimately
77+
quite inaccurate and therefore not useful.</li>
78+
<li>&nbsp;This can be checked by running the program:<br>
79+
<br>
80+
0.49725 <br>
81+
0.97497050625 <br>
82+
0.0951717709512 <br>
83+
0.335845009364 <br>
84+
0.869907242293 <br>
85+
0.441357665188 <br>
86+
0.961588198614 <br>
87+
0.14405170611 <br>
88+
0.4808731671 <br>
89+
0.973573240627<br>
90+
</li>
91+
92+
</ol>
93+
94+
<h2>Programming Exercises</h2>
95+
96+
<ol>
97+
<li>Students should do these interactively
98+
99+
<ol type="a">
100+
<li>Hello, world!</li>
101+
<li>Hello world!</li>
102+
<li>3</li>
103+
<li>3.0</li>
104+
<li>5</li>
105+
<li>5.0</li>
106+
<li>23</li>
107+
<li>2 + 3 = 5</li>
108+
<li>6</li>
109+
<li>8</li>
110+
<li>2.3333333333333335</li>
111+
<li>2</li>
112+
113+
</ol> </li>
114+
115+
<li>This is an activity.</li>
116+
<li>The modified program will print a sequence of values
117+
converging to 0.5 regardless of what value is input. It is no
118+
longer chaotic.</li>
119+
<li>The loop changes to:
120+
<pre>for i in range(20):<br> x = 3.9 * x * (1 - x)<br> print x</pre>
121+
</li>
122+
<li><a href="code/chapter01/c01ex5.py">c01ex5.py</a></li>
123+
<li><a href="code/chapter01/c01ex6.py">c01ex6.py</a></li>
124+
<li><a href="code/chapter01/c01ex7.py">c01ex7.py</a><br>
125+
</li>
126+
127+
</ol>
128+
129+
</body>
130+
</html>

0 commit comments

Comments
 (0)