Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dc8989f

Browse files
Chipe1norvig
authored andcommitted
Replaces max/min with argmax/argmin (aimacode#481)
1 parent ab6669c commit dc8989f

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

search.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,9 @@ def __call__(self, s1): # as of now s1 is a state rather than a percept
544544
self.H[self.s] = min(self.LRTA_cost(self.s, b, self.problem.output(self.s, b),
545545
self.H) for b in self.problem.actions(self.s))
546546

547-
# costs for action b in problem.actions(s1)
548-
costs = [self.LRTA_cost(s1, b, self.problem.output(s1, b), self.H)
549-
for b in self.problem.actions(s1)]
550547
# an action b in problem.actions(s1) that minimizes costs
551-
self.a = list(self.problem.actions(s1))[costs.index(min(costs))]
548+
self.a = argmin(self.problem.actions(s1),
549+
key=lambda b:self.LRTA_cost(s1, b, self.problem.output(s1, b), self.H))
552550

553551
self.s = s1
554552
return self.a

text.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,7 @@ def score(self, plaintext):
318318
def decode(self, ciphertext):
319319
"""Return the shift decoding of text with the best score."""
320320

321-
list_ = [(self.score(shift), shift)
322-
for shift in all_shifts(ciphertext)]
323-
return max(list_, key=lambda elm: elm[0])[1]
321+
return argmax(all_shifts(ciphertext), key=lambda shift: self.score(shift))
324322

325323

326324
def all_shifts(text):
@@ -360,7 +358,7 @@ def decode(self, ciphertext):
360358
problem = PermutationDecoderProblem(decoder=self)
361359
solution = search.best_first_graph_search(
362360
problem, lambda node: self.score(node.state))
363-
print(solution.state, len(solution.state))
361+
364362
solution.state[' '] = ' '
365363
return translate(self.ciphertext, lambda c: solution.state[c])
366364

0 commit comments

Comments
 (0)