Commit d8a67f9a authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

Improvements during solution files handling

parent 61641767
......@@ -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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment