From ed02f65dcae491b5fcae5600f389be612732d25f Mon Sep 17 00:00:00 2001 From: Chipe1 Date: Fri, 7 Apr 2017 09:37:37 +0530 Subject: [PATCH] Replaces max/min with argmax/argmin --- search.py | 6 ++---- text.py | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/search.py b/search.py index c9b6280b4..00ff8a888 100644 --- a/search.py +++ b/search.py @@ -544,11 +544,9 @@ def __call__(self, s1): # as of now s1 is a state rather than a percept self.H[self.s] = min(self.LRTA_cost(self.s, b, self.problem.output(self.s, b), self.H) for b in self.problem.actions(self.s)) - # costs for action b in problem.actions(s1) - costs = [self.LRTA_cost(s1, b, self.problem.output(s1, b), self.H) - for b in self.problem.actions(s1)] # an action b in problem.actions(s1) that minimizes costs - self.a = list(self.problem.actions(s1))[costs.index(min(costs))] + self.a = argmin(self.problem.actions(s1), + key=lambda b:self.LRTA_cost(s1, b, self.problem.output(s1, b), self.H)) self.s = s1 return self.a diff --git a/text.py b/text.py index 37fab1b25..e7f65cd5d 100644 --- a/text.py +++ b/text.py @@ -318,9 +318,7 @@ def score(self, plaintext): def decode(self, ciphertext): """Return the shift decoding of text with the best score.""" - list_ = [(self.score(shift), shift) - for shift in all_shifts(ciphertext)] - return max(list_, key=lambda elm: elm[0])[1] + return argmax(all_shifts(ciphertext), key=lambda shift: self.score(shift)) def all_shifts(text): @@ -360,7 +358,7 @@ def decode(self, ciphertext): problem = PermutationDecoderProblem(decoder=self) solution = search.best_first_graph_search( problem, lambda node: self.score(node.state)) - print(solution.state, len(solution.state)) + solution.state[' '] = ' ' return translate(self.ciphertext, lambda c: solution.state[c])