Skip to content

Commit

Permalink
Use Python for getting the contest problem label in contest formats
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjaclasher authored and Xyene committed Jun 16, 2020
1 parent 65d2ceb commit 56c9644
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
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

0 comments on commit 56c9644

Please sign in to comment.