-
Notifications
You must be signed in to change notification settings - Fork 22
/
stuebenSourceCodeChapter19.py
309 lines (268 loc) · 11.2 KB
/
stuebenSourceCodeChapter19.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
""" +-------------------------------=-------------------------------+
| STUEBEN'S SOURCE CODE: CHAPTER 19 |
| by |
| M. Stueben (November 27, 2017) |
+-------------------------------=-------------------------------+
NOTE: The following programs and functions are from Chapter 19 of
"Good Habits for Writing Code."
"""
###############################<START OF PROGRAM>###############################
# Problem 1 Answers
#=====================<FIVE POSSIBLE ANSWERS>=======================
def upper1(ch): # Bad. It should ignore non-lowercase letters.
return chr(ord(ch) - 32)
#------------------------------------------------------------
def upper2(ch): # BAD: It aborts program.
if 'a' <= ch <= 'z':
return chr(ord(ch) - 32)
exit('ERROR: Bad input = ' + str(ch))
#------------------------------------------------------------
def upper3(ch): # BAD: It returns TWO different data types.
if 'a' <= ch <= 'z':
return chr(ord(ch) - 32)
return -1
#------------------------------------------------------------
def upper4(ch): # OK, however, the error traps are unnecessary.
assert type(ch) == str and len(ch) == 1, ch
if 'a' <= ch <= 'z':
ch = chr(ord(ch) - 32)
return ch
#------------------------------------------------------------
def upper5(ch): # Best: 1. It ignores non-lowercase letters.
# 2. It returns only one data type.
# 3. It has no needless error traps.
if 'a' <= ch <= 'z':
ch = chr(ord(ch) - 32)
return ch
#-------------------------------------------------------------------Chapter 19--
# Problem 2 Answers
#
def equal1(num1, num2): # Terrible code
#---Check the data
if not isinstance(num1, (int, float)) or \
not isinstance(num2, (int, float)):
return None
#---Return equality (True or false)
if abs(num1 - num2) < 0.000000000001:
return True
return False
def equal2(x, y): # Ex.: equals2(0.000 000 000 01, 0) is False,
# but equals2(0.000 000 000 001, 0) is True.
return abs(x-y) <= 1e-12 # 1e-12 = 0.000 000 000 001 (eleven decimal zeros)
def equal3(x, y): # Ex.: equals3(0.000 000 000 01, 0) is False,
# but equals3(0.000 000 000 001, 0) is True.
return round (x, 11) == round (y, 11) # 1e-12 = 0.000 000 000 001 = 1 billionth
#-------------------------------------------------------------------Chapter 19--
# Problem 3 My Answer
def solveBertrandsParadox():
#---Initialize.
from random import randint
trials = 100000
goldFirst = 0
goldMatch = 0
coin = [['gold', 'gold' ],
['gold', 'silver'],
['silver','silver'],]
#---Run many simulation trials.
for n in range(trials):
drawer = randint(0,2)
position = randint(0,1)
if coin[drawer][position] == 'silver':
continue
goldFirst += 1
if coin[drawer][position] == coin[drawer][1-position]:
goldMatch += 1
#---Print labeled answer.
print('Six coin answer for', trials, 'trials:',
round(goldMatch/goldFirst * 100, 1), '%')
#-------------------------------------------------------------------Chapter 19--
# Problem 4 Answers
"""
VERSION 4a. Two break points are randomly marked on the given stick, and
the stick is broken into three parts.
"""
def puzzle4a():
triangleCount = 0
for n in range(TOTAL_RUNS):
a, b = random(), random()
if a > b:
a, b = b, a # a = length of left piece
if (a < 0.5 and b-a < 0.5 and b > 0.5): # b-a = length of middle piece.
triangleCount += 1 # 1-b = length of right piece.
print('Puzzle 6a: The probability of forming a triangle is',
round( triangleCount/TOTAL_RUNS, 3) )
#---Output: Probability of forming a triangle is +------+ in 4.39 seconds.
# | 0.25 |
# +------+
#-----------------------------------------------------------computer simulation--
"""
VERSION 4b. One break point is randomly marked on the given stick. The stick
is broken into two parts. A second break point is marked on the
longer of the two sticks. That stick is broken.
"""
#----------------------------------------------------------computer simulation--
def puzzle4b():
triangleCount = 0
for n in range(TOTAL_RUNS):
a = random()
if a < 0.5:
b = uniform(a, 1)
else:
b = a
a = uniform(0, b)
if (a < 0.5 and b-a < 0.5 and b > 0.5): # a < b
triangleCount += 1
print('Puzzle 4b: The probability of forming a triangle is',
round( triangleCount/TOTAL_RUNS, 3) )
#---Output: Probability of forming a triangle is +-------+ in 8.3 seconds.
# | 0.386 |
# +-------+
#----------------------------------------------------------computer simulation--
"""
VERSION 4c. One break point is randomly marked on the given stick.
The stick is broken. One of the sticks is randomly chosen,
and a second break point is marked on it. That stick is broken.
"""
def puzzle4c():
triangleCount = 0
for n in range(TOTAL_RUNS):
r = random() # r = first break point
if random() < 0.5: # flip a coin
a = uniform(0, r) # cut on the left side
b = r
else:
a = r # cut on the right side
b = uniform(r, 1)
if (a < 0.5 and b-a < 0.5 and b > 0.5): # a < b
triangleCount += 1
print('Puzzle 4c: The probability of forming a triangle is',
round( triangleCount/TOTAL_RUNS, 3) )
#---Output: Probability of forming a triangle is +-------+ in 8.70 seconds.
# | 0.193 |
# +-------+
#----------------------------------------------------------computer simulation--
"""
VERSION 4d. One break point is randomly marked on the given stick. The stick
is broken. One of the sticks is randomly WITHN A PROBABILITY
PROPORTIONAL TO ITS LENGTH, and a second break point is marked
on it. That stick is broken.
"""
def puzzle4d():
triangleCount = 0
for n in range(TOTAL_RUNS):
r = random() # r = first break point
if random() < r: # break left stick
a = uniform(0, r)
b = r
else: # break right stick
a = r
b = uniform(r, 1)
if (a < 0.5 and b-a < 0.5 and b > 0.5): # a < b
triangleCount += 1
print('Puzzle 4d: The probability of forming a triangle is',
round( triangleCount/TOTAL_RUNS, 3) )
#---Output: Probability of forming a triangle is +-------+ in 8.68 seconds
# | 0.25 |
# +-------+
#-------------------------------------------------------------------Chapter 19--
# Problem 5 My Answer
def permute(Lst, r):
from math import factorial
L = len(Lst)
assert L>=1 and r>=0 and r<factorial(L), ['L=', L, 'r=', r]
Lst = Lst[:]
if L == 1: return Lst
d = factorial(L-1)
digit = Lst[r//d]
Lst.remove(digit)
return [digit] + permute(Lst, r%d)
#-------------------------------------------------------------------Chapter 19--
# Problem 6 Answers
#
#--Solution 1 Best, because it is so easy to debug.
for x in range(1,101):
if x % 15 == 0: print('Fizz and Buzz'); continue
if x % 3 == 0: print('Fizz'); continue
if x % 5 == 0: print('Buzz'); continue
print(x)
#-------------------------------------------------------------
#--Solution 2 Mr. Stueben's solution.
for x in range(1, 101):
if x % 15 == 0: print('Fizz and Buzz')
if x % 3 == 0 and x % 5 != 0: print('Fizz')
if x % 5 == 0 and x % 3 != 0: print('Buzz')
if x % 5 != 0 and x % 3 != 0: print(x)
#-------------------------------------------------------------
#--Solution 3 Not bad.
for x in range(1, 101):
if x % 15 == 0:
print('Fizz and Buzz')
elif x % 3 == 0:
print('Fizz')
elif x % 5 == 0:
print('Buzz')
else:
print(x)
#-------------------------------------------------------------
#--Solution 4 Clever.
for x in range(1, 101):
stng = ''
if x % 3 == 0: stng += 'Fizz'
if x % 15 == 0: stng += ' and '
if x % 5 == 0: stng += 'Buzz'
print(stng if stng else x)
#-------------------------------------------------------------
#--Solution 5 Maybe too clever.
for x in range(1, 101):
stng = 'Fizz and Buzz' if x%15 == 0 \
else 'Fizz' if x% 3 == 0 \
else 'Buzz' if x% 5 == 0 \
else ''
print(stng if stng else x)
#-------------------------------------------------------------
#--Solution 6 # The “not†makes the code more difficult to understand.
for n in range (101):
stng = str(n)
if not(n%3): stng = 'Fizz'
if not(n%5): stng = 'Buzz'
if not(n%3 + n%5):
stng ='Fizz and Buzz'
print(n, stng)
#-------------------------------------------------------------
#--Solution 7 This code says much about the programmer’s lack
# of experience in refactoring.
for n in range(1,101):
flag = True
if n%3 == 0:
print('Fizz', end = '')
if n%15 == 0:
print(' and Buzz', end = '')
print()
flag = False
if flag and n%5 == 0:
print('Buzz')
flag = False
if flag:
print(n)
#-------------------------------------------------------------
#--Solution 8 Why would anyone work with x+1 instead of x? Why would
# anyone write “if (x+1) % 3 == 0: if (x+1) % 5 == 0â€Â,
# instead of a single “if (x+1) % 15 == 0�
for x in range(100):
if (x+1) % 3 ==0:
if (x+1) % 5 == 0:
print('Fizz and Buzz')
else:
print('Fizz')
elif (x+1) % 5 == 0:
print('Buzz')
else:
print((x+1))
#=====================================<MAIN>====================================
def main():
pass
#-------------------------------------------------------------------Chapter 19--
if __name__ == '__main__':
from time import clock; START_TIME = clock(); main(); print('- '*12);
print('RUN TIME:%6.2f'%(clock()-START_TIME), 'seconds.');
################################<END OF PROGRAM>################################