Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Python for getting the contest problem label in contest formats #1453

Merged
merged 1 commit into from
Jun 16, 2020
Merged
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
7 changes: 4 additions & 3 deletions judge/contest_format/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ def get_problem_breakdown(self, participation, contest_problems):
raise NotImplementedError()

@abstractmethod
def get_contest_problem_label_script(self):
def get_label_for_problem(self, index):
"""
Returns the default Lua script to generate contest problem labels.
Returns the problem label for a given zero-indexed index.

:return: A string, the Lua script.
:param index: The zero-indexed problem index.
:return: A string, the problem label.
"""
raise NotImplementedError()

Expand Down
8 changes: 2 additions & 6 deletions judge/contest_format/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,5 @@ def display_participation_result(self, participation):
def get_problem_breakdown(self, participation, contest_problems):
return [(participation.format_data or {}).get(str(contest_problem.id)) for contest_problem in contest_problems]

def get_contest_problem_label_script(self):
return '''
function(n)
return tostring(math.floor(n + 1))
end
'''
def get_label_for_problem(self, index):
return str(index + 1)
19 changes: 7 additions & 12 deletions judge/contest_format/icpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,10 @@ def display_user_problem(self, participation, contest_problem):
else:
return mark_safe('<td></td>')

def get_contest_problem_label_script(self):
return '''
function(n)
n = n + 1
ret = ""
while n > 0 do
ret = string.char((n - 1) % 26 + 65) .. ret
n = math.floor((n - 1) / 26)
end
return ret
end
'''
def get_label_for_problem(self, index):
index += 1
ret = ''
while index > 0:
ret += chr((index - 1) % 26 + 65)
index = (index - 1) // 26
return ret[::-1]
5 changes: 4 additions & 1 deletion judge/models/contest.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,13 @@ def format(self):

@cached_property
def get_label_for_problem(self):
if not self.problem_label_script:
return self.format.get_label_for_problem

def DENY_ALL(obj, attr_name, is_setting):
raise AttributeError()
lua = LuaRuntime(attribute_filter=DENY_ALL, register_eval=False, register_builtins=False)
return lua.eval(self.problem_label_script or self.format.get_contest_problem_label_script())
return lua.eval(self.problem_label_script)

def clean(self):
# Django will complain if you didn't fill in start_time or end_time, so we don't have to.
Expand Down