Skip to content

Commit

Permalink
import_polygon_package: normalize fractional points (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
hieplpvip authored Aug 25, 2023
1 parent bb93da3 commit e809bfc
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions judge/management/commands/import_polygon_package.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import hashlib
import json
import math
import os
import re
import shutil
Expand Down Expand Up @@ -341,7 +342,7 @@ def parse_tests(problem_meta, root, package):
if groups is not None:
for group in groups.getchildren():
name = group.get('name')
points = int(float(group.get('points', 0)))
points = float(group.get('points', 0))
points_policy = group.get('points-policy')
dependencies = group.find('dependencies')
if dependencies is None:
Expand All @@ -365,7 +366,7 @@ def parse_tests(problem_meta, root, package):
input_path_pattern = testset.find('input-path-pattern').text
answer_path_pattern = testset.find('answer-path-pattern').text
for i, test in enumerate(testset.find('tests').getchildren()):
points = int(float(test.get('points', 0)))
points = float(test.get('points', 0))
input_path = input_path_pattern % (i + 1)
answer_path = answer_path_pattern % (i + 1)
input_file = f'{(i + 1):02d}.inp'
Expand Down Expand Up @@ -414,6 +415,20 @@ def get_tests_by_batch(name):
for batch in each_test_batches:
del problem_meta['batches'][batch]

# Normalize points if necessary
# Polygon allows fractional points, but DMOJ does not
all_points = [batch['points'] for batch in problem_meta['batches'].values()] + \
[problem_meta['cases_data'][i]['points'] for i in problem_meta['normal_cases']]
if any(not p.is_integer() for p in all_points):
print('Found fractional points. Normalize to integers')
all_points = [int(p * 1000) for p in all_points]
gcd = math.gcd(*all_points)
for batch in problem_meta['batches'].values():
batch['points'] = int(batch['points'] * 1000) // gcd
for i in problem_meta['normal_cases']:
case_data = problem_meta['cases_data'][i]
case_data['points'] = int(case_data['points'] * 1000) // gcd

# Ignore zero-point batches
zero_point_batches = [name for name, batch in problem_meta['batches'].items() if batch['points'] == 0]
if len(zero_point_batches) > 0:
Expand Down

0 comments on commit e809bfc

Please sign in to comment.