From 56c96442b3cbd6c03f5f7ffa7ae116d16dc4f20b Mon Sep 17 00:00:00 2001 From: Evan Date: Sun, 14 Jun 2020 14:38:56 -0400 Subject: [PATCH] Use Python for getting the contest problem label in contest formats --- judge/contest_format/base.py | 7 ++++--- judge/contest_format/default.py | 8 ++------ judge/contest_format/icpc.py | 19 +++++++------------ judge/models/contest.py | 5 ++++- 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/judge/contest_format/base.py b/judge/contest_format/base.py index 819e122368..0b70fb2c23 100644 --- a/judge/contest_format/base.py +++ b/judge/contest_format/base.py @@ -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() diff --git a/judge/contest_format/default.py b/judge/contest_format/default.py index 5ca0f9fa41..25c1fd41f1 100644 --- a/judge/contest_format/default.py +++ b/judge/contest_format/default.py @@ -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) diff --git a/judge/contest_format/icpc.py b/judge/contest_format/icpc.py index 855a34b151..3e1918de8a 100644 --- a/judge/contest_format/icpc.py +++ b/judge/contest_format/icpc.py @@ -115,15 +115,10 @@ def display_user_problem(self, participation, contest_problem): else: return mark_safe('') - 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] diff --git a/judge/models/contest.py b/judge/models/contest.py index c8485083b0..97be306ae7 100644 --- a/judge/models/contest.py +++ b/judge/models/contest.py @@ -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.