diff --git a/solution_sort.py b/solution_sort.py index ce943a29ff957dbcfee2e683a00d452f0714a7f1..3d87044c6390cdf7c9d401696225aa0de3067558 100644 --- a/solution_sort.py +++ b/solution_sort.py @@ -3,22 +3,36 @@ from __future__ import unicode_literals from __future__ import print_function +def get_solutions(file_descriptor): + """return a tuple of the original line + the stripped line containing the solution""" + + for line in file_descriptor: + # Remove possible \t separator from first line (frontier solution) + line = line.rstrip('\n') + stripped_line = line.rstrip('\t').replace('\t', ' ') + + # Next Line if empty + if stripped_line == '': + continue + + if stripped_line[0] not in ('%', '=', ' '): + # print(stripped_line) + # Sort in lower case, remove ' ' empty elements + yield line, stripped_line + + def sort_solutions(file): """Sort all solutions in alphabetical order in place.""" solutions = dict() with open(file, 'r+') as fd: - for line in fd: - line = line.rstrip('\n') - # Remove possible \t separator from first line (frontier solution) - stripped_line = line.rstrip('\t').replace('\t', ' ') - - if stripped_line[0] not in ('%', '=', ' ', ''): - # print(stripped_line) - # Sort in lower case, remove ' ' empty elements - solutions[line] = \ - " ".join(sorted([place for place in stripped_line.split(' ') if place != ' '], key=lambda s: s.lower())) + + for line, stripped_line in get_solutions(fd): + # Sort in lower case, remove ' ' empty elements + solutions[line] = \ + " ".join(sorted([place for place in stripped_line.split(' ') + if place != ' '], key=lambda s: s.lower())) # Rewind fd.seek(0)