Skip to content

Commit

Permalink
simplify & cleanup Problem.testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
RagnarGrootKoerkamp committed Feb 8, 2024
1 parent 6d069ad commit c45e8f3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 33 deletions.
2 changes: 1 addition & 1 deletion bin/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def build_samples_zip(problems, statement_language):

# Add samples for non-interactive problems.
if not problem.interactive:
samples = problem.testcases(only_sample=True)
samples = problem.testcases(only_samples=True)
for i in range(0, len(samples)):
sample = samples[i]
basename = outputdir / str(i + 1)
Expand Down
11 changes: 4 additions & 7 deletions bin/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def create_samples_file(problem):
samples_data = ''

for sample in samples:
if isinstance(sample, Path) and sample.suffix == '.interaction':
if isinstance(sample, Path):
assert sample.suffix == '.interaction'
interaction_dir = builddir / 'interaction'
interaction_dir.mkdir(exist_ok=True)

Expand Down Expand Up @@ -71,12 +72,8 @@ def flush():
last = line[0]
flush()
else:
# Already handled above.
if sample.in_path.with_suffix('.interaction').is_file():
continue
samples_data += (
f'\\Sample{{{sample.in_path.as_posix()}}}{{{sample.ans_path.as_posix()}}}\n'
)
(in_path, ans_path) = sample
samples_data += f'\\Sample{{{in_path.as_posix()}}}{{{ans_path.as_posix()}}}\n'
samples_file_path.write_text(samples_data)


Expand Down
34 changes: 9 additions & 25 deletions bin/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,25 +180,19 @@ def get_testdata_yaml(p, path):
def testcases(
p,
*,
needans=True,
needinteraction=False,
only_sample=False,
statement_samples=False,
mode=None,
copy=False,
needans=True,
only_samples=False,
):
def maybe_copy(x):
return x.copy() if copy and isinstance(x, (list, dict)) else x
only_samples = config.args.samples or only_samples

samplesonly = config.args.samples or only_sample

key = (needans, samplesonly)
key = (needans, only_samples)
if key in p._testcases is not None:
return maybe_copy(p._testcases[key])
return p._testcases[key]

in_paths = None
if config.args.testcases:
if samplesonly:
if only_samples:
assert False
# Deduplicate testcases with both .in and .ans.
in_paths = []
Expand All @@ -223,19 +217,12 @@ def maybe_copy(x):
in_paths += glob(p.path, f'data/{prefix}/**/*.in')
else:
in_paths = list(glob(p.path, 'data/sample/**/*.in'))
if statement_samples:
in_paths += list(glob(p.path, 'data/sample/**/*.in.statement'))
if not samplesonly:
if not only_samples:
in_paths += list(glob(p.path, 'data/secret/**/*.in'))

testcases = []
for f in in_paths:
t = testcase.Testcase(p, f)
# Require both in and ans files
if needinteraction and not t.in_path.with_suffix('.interaction').is_file():
assert only_sample
warn(f'Found input file {f} without a .interaction file. Skipping.')
continue
if needans and not t.ans_path.is_file():
if t.root != 'invalid_inputs':
warn(f'Found input file {f} without a .ans file. Skipping.')
Expand All @@ -244,14 +231,11 @@ def maybe_copy(x):
testcases.sort(key=lambda t: t.name)

if len(testcases) == 0:
if needinteraction:
warn(f'Didn\'t find any testcases with interaction for {p.name}')
else:
warn(f'Didn\'t find any testcases{" with answer" if needans else ""} for {p.name}')
warn(f'Didn\'t find any testcases{" with answer" if needans else ""} for {p.name}')
testcases = False

p._testcases[key] = testcases
return maybe_copy(testcases)
return testcases

# Returns a list of:
# - (Path, Path) = (.in, .ans) pairs
Expand Down

0 comments on commit c45e8f3

Please sign in to comment.