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 991c764d9..ce01e6184 100644 --- a/text.py +++ b/text.py @@ -60,7 +60,7 @@ def add_sequence(self, words): n = self.n words = self.add_empty(words, n) - for i in range(len(words) - n): + for i in range(len(words) - n + 1): self.add(tuple(words[i:i + n])) def samples(self, nwords): @@ -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): @@ -380,8 +378,9 @@ def __init__(self, initial=None, goal=None, decoder=None): def actions(self, state): # Find the best - p, plainchar = max([(self.decoder.P1[c], c) - for c in alphabet if c not in state]) + + search_list = [c for c in alphabet if c not in state] + plainchar = argmax(search_list, key=lambda c: self.decoder.P1[c]) succs = [extend(state, plainchar, cipherchar)] # ???? # noqa def goal_test(self, state):