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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy