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 cf99d17

Browse files
author
Clinton Sheppard
authored
Merge pull request #4 from GregEakin/master
Fix typos
2 parents 9ab98ed + 54078b4 commit cf99d17

File tree

6 files changed

+24
-16
lines changed

6 files changed

+24
-16
lines changed

‎ch07/knightsTests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_3x4(self):
9393
width = 4
9494
height = 3
9595
# 1,0 2,0 3,0
96-
# 0,2 1,2 2,0
96+
# 0,2 1,2 2,2
9797
# 2 N N N .
9898
# 1 . . . .
9999
# 0 . N N N
@@ -111,8 +111,8 @@ def test_10x10(self):
111111
self.find_knight_positions(width, height, 22)
112112

113113
def test_12x12(self):
114-
width = 13
115-
height = 13
114+
width = 12
115+
height = 12
116116
self.find_knight_positions(width, height, 28)
117117

118118
def test_13x13(self):

‎ch09/knapsackTests.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,11 @@ def __init__(self, totalWeight, totalVolume, totalValue):
253253
self.TotalValue = totalValue
254254

255255
def __gt__(self, other):
256-
return self.TotalValue > other.TotalValue
256+
if self.TotalValue != other.TotalValue:
257+
return self.TotalValue > other.TotalValue
258+
if self.TotalWeight != other.TotalWeight:
259+
return self.TotalWeight < other.TotalWeight
260+
return self.TotalValue < other.TotalValue
257261

258262
def __str__(self):
259263
return "wt: {:0.2f} vol: {:0.2f} value: {}".format(

‎ch13/approximatePiTests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def fnMutate(genes):
9090
best = genetic.get_best(fnGetFitness, length, optimalFitness,
9191
geneset, fnDisplay, fnMutate, maxAge=250,
9292
maxSeconds=maxSeconds)
93-
return best.Fitness>=optimalFitness
93+
return optimalFitness<=best.Fitness
9494

9595
def test_optimize(self):
9696
geneset = [i for i in range(1, 512 + 1)]

‎ch15/lawnmowerTests.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def mutate(genes, geneSet, minGenes, maxGenes, fnGetFitness, maxRounds):
4545
count -= 1
4646
if fnGetFitness(genes) > initialFitness:
4747
return
48+
4849
adding = len(genes) == 0 or \
4950
(len(genes) < maxGenes and random.randint(0, 5) == 0)
5051
if adding:
@@ -236,7 +237,7 @@ def fnMutate(child):
236237
fnDisplay, fnMutate, fnCreate, maxAge=None,
237238
poolSize=10, crossover=crossover)
238239

239-
self.assertTrue(not best.Fitness > optimalFitness)
240+
self.assertTrue(not optimalFitness > best.Fitness)
240241

241242

242243
class Mow:
@@ -342,6 +343,7 @@ def __init__(self, genes):
342343

343344
if type(temp[index]) is Call:
344345
temp[index].Funcs = funcs
346+
345347
if type(temp[index]) is Func:
346348
if len(funcs) > 0 and not temp[index].ExpectCall:
347349
temp[index] = Call()
@@ -369,14 +371,14 @@ def __init__(self, genes):
369371
len(funcs[func_id].Ops) == 0:
370372
del func.Ops[index]
371373

372-
for index in reversed(range(len(temp))):
373-
if type(temp[index]) is Call:
374-
func_id = temp[index].FuncId
375-
if func_id is None:
376-
continue
377-
if func_id >= len(funcs) or \
378-
len(funcs[func_id].Ops) == 0:
379-
del temp[index]
374+
for index in reversed(range(len(temp))):
375+
if type(temp[index]) is Call:
376+
func_id = temp[index].FuncId
377+
if func_id is None:
378+
continue
379+
if func_id >= len(funcs) or \
380+
len(funcs[func_id].Ops) == 0:
381+
del temp[index]
380382
self.Main = temp
381383
self.Funcs = funcs
382384

‎ch17/regexTests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ def __init__(self, numWantedMatched, totalWanted, numUnwantedMatched,
357357
def __gt__(self, other):
358358
combined = (self._totalWanted - self.NumWantedMatched) \
359359
+ self.NumUnwantedMatched
360-
otherCombined = (self._totalWanted - other.NumWantedMatched) \
360+
otherCombined = (other._totalWanted - other.NumWantedMatched) \
361361
+ other.NumUnwantedMatched
362362
if combined != otherCombined:
363363
return combined < otherCombined
@@ -370,7 +370,7 @@ def __gt__(self, other):
370370
return self.Length < other.Length
371371

372372
def __str__(self):
373-
return "matches {} wanted {} unwanted, len {}".format(
373+
return "matches: {} wanted, {} unwanted, len {}".format(
374374
"all" if self._totalWanted == self.NumWantedMatched else self.NumWantedMatched,
375375
self.NumUnwantedMatched,
376376
self.Length)

‎ch18/ticTacToeTests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ def mutate_move(genes):
200200
toMove = genes[start:stop]
201201
genes[start:stop] = []
202202
index = random.choice(range(len(genes)))
203+
if index >= start
204+
index += 1
203205
genes[index:index] = toMove
204206
return True
205207

0 commit comments

Comments
(0)

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