Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a42da7f

Browse files
Solutions
1 parent bca7fa4 commit a42da7f

Some content is hidden

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

56 files changed

+1504
-0
lines changed

‎CH5/Checkpoints

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#5.1
2+
At point A, count<100 is always true.
3+
At point B, count<100 is sometimes true.
4+
At point C, count<100 is always false.
5+
6+
#5.2
7+
guess may be equal to number, so the while loop is never executed.
8+
9+
#5.3
10+
(a) Infinite loop, no printout
11+
(b) Infinite loop, no printout
12+
(c) 9 times, 2,4,6,8
13+
14+
#5.4
15+
(a) count variable is not incremented, infinite loop.
16+
(b) count variable is decremented instead of increment, infinite loop.
17+
(c) count+=1 must be indented inside the loop.
18+
19+
#5.5
20+
max is 5
21+
number 0
22+
23+
#5.6
24+
sum is 14
25+
count is 4
26+
27+
#5.7
28+
Yes. The advantages of for loops are simplicity and readability.
29+
Compilers can produce more efficient code for the for loop than for the corresponding while loop
30+
31+
#5.8
32+
sum = 0
33+
i= 0
34+
while i <= 1000:
35+
sum += i
36+
i += 1
37+
38+
#5.9
39+
Not possible for all cases. For example, you cannot convert the while loop in Listing 5.3, GuessNumber.py, to a for loop.
40+
sum = 0
41+
for i in range(1, 10000):
42+
if sum < 10000:
43+
sum = sum + i
44+
45+
#5.10
46+
(a) n-1
47+
(b) n-1
48+
(c) n-5
49+
(d) The ceiling of (n-5)/3
50+
51+
#5.11
52+
(a) 0 0 1 0 1 2 0 1 2 3
53+
(b)
54+
****
55+
****
56+
2 ****
57+
3 2 ****
58+
4 3 2 ****
59+
(c)
60+
1xxx2xxx4xxx8xxx16xxx
61+
1xxx2xxx4xxx8xxx
62+
1xxx2xxx4xxx
63+
1xxx2xxx
64+
1xxx
65+
(d)
66+
1G
67+
1G3G
68+
1G3G5G
69+
1G3G5G7G
70+
1G3G5G7G9G
71+
72+
#5.12
73+
It may give floating point number, for example if n1 = 3 and n2 = 1
74+
75+
#5.13
76+
The keyword break is used to exit the current loop. The program in (A) will terminate. The output is Balance is 1.
77+
78+
The keyword continue causes the rest of the loop body to be skipped for
79+
the current iteration. The while loop will not terminate in (B).
80+
81+
#5.14
82+
If a continue statement is executed inside a for loop,
83+
the rest of the iteration is skipped, then the action-after-each-iteration
84+
is performed and the loop-continuation-condition is checked.
85+
If a continue statement is executed inside a while loop,
86+
the rest of the iteration is skipped, then the loop-continuation-condition is checked.
87+
88+
fix:
89+
90+
i = 0
91+
92+
while i < 4:
93+
if i % 3 == 0:
94+
i += 1
95+
continue
96+
sum += i
97+
i += 1
98+
99+
#5.15
100+
TestBreak:
101+
sum = 0
102+
number = 0
103+
104+
while number < 20 and sum < 100:
105+
number += 1
106+
sum += number
107+
108+
print("The number is " + str(number))
109+
print("The sum is " + str(sum))
110+
111+
TestContinue:
112+
113+
sum = 0
114+
number = 0
115+
116+
while (number < 20):
117+
number += 1
118+
if (number != 10 and number != 11):
119+
sum += number
120+
121+
print("The sum is " + str(sum))
122+
123+
124+
#5.16
125+
(a) print(i)
126+
1
127+
2
128+
1
129+
2
130+
2
131+
3
132+
(b)for j in range (1,4):
133+
1
134+
2
135+
1
136+
2
137+
2
138+
3
139+

‎CH5/EX5.1.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 5.1 (Count positive and negative numbers and compute the average of numbers)
2+
# Write a program that reads an unspecified number of integers, determines
3+
# how many positive and negative values have been read, and computes the total
4+
# and average of the input values (not counting zeros). Your program ends with the
5+
# input 0. Display the average as a floating-point number.
6+
7+
avg = 0
8+
postives = 0
9+
negativies = 0
10+
total = 0
11+
n = int(input("Enter an integer, the input ends if it is 0: "))
12+
13+
if n == 0:
14+
print("You didn't enter any number")
15+
else:
16+
while n != 0:
17+
if n > 0:
18+
postives += 1
19+
else:
20+
negativies += 1
21+
total += n
22+
avg = total / (postives + negativies)
23+
n = int(input("Enter an integer, the input ends if it is 0: "))
24+
25+
print("The number of positives is", postives)
26+
print("The number of negatives is", negativies)
27+
print("The total is", total)
28+
print("The average is", avg)

‎CH5/EX5.10.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 5.10 (Find the highest score) Write a program that prompts the user to enter the number
2+
# of students and each student’s score, and displays the highest score. Assume that
3+
# the input is stored in a file named score.txt, and the program obtains the input from
4+
# the file.
5+
6+
studentName = ""
7+
studentScore = 0
8+
maxScore = -1
9+
maxName = ""
10+
while True:
11+
studentName = input("Enter a student name(x to stop): ")
12+
if studentName.lower() == "x":
13+
break
14+
studentScore = eval(input("Enter a student score: "))
15+
16+
if studentScore > maxScore:
17+
maxScore = studentScore
18+
maxName = studentName
19+
20+
print("Top student is", maxName, "with score", maxScore)

‎CH5/EX5.11.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 5.11 (Find the two highest scores) Write a program that prompts the user to enter the
2+
# number of students and each student’s score, and displays the highest and secondhighest
3+
# scores.
4+
5+
maxScroe1 = 0
6+
maxScore2 = -1
7+
maxName1 = maxName2 = ""
8+
9+
while True:
10+
name = input("Enter Student's name(x to Stop): ")
11+
if name.lower() == "x":
12+
break
13+
score = eval(input("Enter student's score: "))
14+
15+
if score > maxScroe1 and score > maxScore2:
16+
maxScore2 = maxScroe1
17+
maxScroe1 = score
18+
maxName2 = maxName1
19+
maxName1 = name
20+
elif score > maxScore2:
21+
maxScore2 = score
22+
maxName2 = name
23+
24+
print("Top Student is", maxName1, "with score", maxScroe1)
25+
print("The 2nd Top student is", maxName2, "with score", maxScore2)

‎CH5/EX5.12.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 5.12 (Find numbers divisible by 5 and 6) Write a program that displays, ten numbers
2+
# per line, all the numbers from 100 to 1,000 that are divisible by 5 and 6. The numbers
3+
# are separated by exactly one space.
4+
5+
counter = 0
6+
for i in range(100, 1000):
7+
if i % 5 == 0 and i % 6 == 0:
8+
if counter == 10:
9+
print()
10+
counter = 0
11+
12+
print(i, end=" ")
13+
counter += 1

‎CH5/EX5.13.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 5.13 (Find numbers divisible by 5 or 6, but not both) Write a program that displays, ten
2+
# numbers per line, all the numbers from 100 to 200 that are divisible by 5 or 6, but
3+
# not both. The numbers are separated by exactly one space.
4+
5+
counter = 0
6+
for i in range(100, 200):
7+
if i % 5 == 0 or i % 6 == 0 and not (i % 5 == 0 and i % 6 == 0):
8+
if counter == 10:
9+
print()
10+
counter = 0
11+
print(i, end=" ")
12+
counter += 1

‎CH5/EX5.14.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 5.14 (Find the smallest n such that n2 12,000) Use a while loop to find the smallest
2+
# integer n such that n2 is greater than 12,000.
3+
4+
n = 1
5+
while True:
6+
if n ** 2 > 12000:
7+
break
8+
n += 1
9+
10+
print("The smallest integer n such that n^2 > 12,000 is", n)

‎CH5/EX5.15.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# 5.15 (Find the largest n such that n3 12,000) Use a while loop to find the largest
2+
# integer n such that n3 is less than 12,000.
3+
4+
n = 12000
5+
while True:
6+
if n ** 3 < 12000:
7+
break
8+
n -= 1
9+
10+
print("the largest n such that n^3 < 12,000 is", n)

‎CH5/EX5.16.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 5.16 (Compute the greatest common divisor) For Listing 5.8, another solution to find
2+
# the greatest common divisor of two integers n1 and n2 is as follows: First find d to
3+
# be the minimum of n1 and n2, and then check whether d, d - 1, d - 2, ..., 2, or
4+
# 1 is a divisor for both n1 and n2 in this order. The first such common divisor is the
5+
# greatest common divisor for n1 and n2.
6+
7+
n1 = int(input("Enter first integer: "))
8+
n2 = int(input("Enter second integer: "))
9+
10+
d = min(n1, n2)
11+
12+
while True:
13+
if n1 % d == 0 and n2 % d == 0:
14+
break
15+
d -= 1
16+
17+
print("The greatest common divisor is", d)

‎CH5/EX5.17.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 5.17 (Display the ASCII character table) Write a program that displays the characters
2+
# in the ASCII character table from ! to ~. Display ten characters per line. The characters
3+
# are separated by exactly one space.
4+
5+
counter = 0
6+
for i in range(ord('!'), ord('~')):
7+
if counter == 10:
8+
print()
9+
counter = 0
10+
print(chr(i), end=" ")
11+
counter+=1

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /