Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Construction:

@classmethod
def from_txt(cls, data: str) -> Construction:
data = data.split(' ')
data = data.strip().split()
return Construction(data[0], data[1:])

def __init__(self, name: str, args: list[str]):
Expand All @@ -71,12 +71,13 @@ class Clause:

@classmethod
def from_txt(cls, data: str) -> Clause:
if data == ' =':
data = data.strip()
if data == '=':
return Clause([], [])
points, constructions = data.split(' = ')
points, constructions = data.split('=', maxsplit=1)
return Clause(
points.split(' '),
[Construction.from_txt(c) for c in constructions.split(', ')],
points.strip().split(),
[Construction.from_txt(c) for c in constructions.split(',')],
)

def __init__(self, points: list[str], constructions: list[Construction]):
Expand Down Expand Up @@ -162,15 +163,15 @@ def from_txt(cls, data: str, translate: bool = True) -> Problem:
"""Load a problem from a str object."""
url = ''
if '\n' in data:
url, data = data.split('\n')
url, data = data.split('\n', maxsplit=1)

if ' ? ' in data:
clauses, goal = data.split(' ? ')
if '?' in data:
clauses, goal = data.split('?', maxsplit=1)
goal = Construction.from_txt(goal)
else:
clauses, goal = data, None

clauses = clauses.split('; ')
clauses = [c.strip() for c in clauses.split(';') if c.strip()]
problem = Problem(
url=url, clauses=[Clause.from_txt(c) for c in clauses], goal=goal
)
Expand Down
15 changes: 15 additions & 0 deletions problem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ def test_orthocenter_no_translate(self):
'{S} a : ; b : ; c : ; h : T a b c h 00 T a c b h 01 ? T a h b c',
)

def test_problem_parser_allows_whitespace_around_delimiters(self):
canonical = (
'a b c = triangle a b c; h = on_tline h b a c, '
'on_tline h c a b ? perp a h b c'
)
irregular = (
' a b c=triangle a b c ;h = on_tline h b a c, '
'on_tline h c a b; ?perp a h b c '
)

expected = pr.Problem.from_txt(canonical, translate=False)
actual = pr.Problem.from_txt(irregular, translate=False)

self.assertEqual(actual.txt(), expected.txt())

def test_orthocenter_translate(self):
txt = 'a b c = triangle a b c; h = on_tline h b a c, on_tline h c a b ? perp a h b c' # pylint: disable=line-too-long

Expand Down