Last active
May 13, 2025 23:47
-
-
Save ChuckieChen945/e9bd4e07d6df4f051c56eceaa3a14fe5 to your computer and use it in GitHub Desktop.
配合 LeetCode Editor 插件使用的 LeetCode Python 刷题模板,可生成一些手脚架代码自动输入测试用例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# region init | |
from typing import * | |
import math | |
from collections import defaultdict | |
class ListNode: | |
pass | |
# endregion init | |
${question.code} | |
# region auto_test | |
question_content = ''' | |
${question.content} | |
''' | |
question_code = ''' | |
$!velocityTool.snakeCaseName(${question.code}) | |
''' | |
# Definition for singly-linked list. | |
class ListNode: | |
def __init__(self, val=0, next=None): | |
self.val = val | |
self.next = next | |
def is_input_linked_list(str_question: str) -> bool: | |
import operator | |
return operator.contains(str_question, "#_definition_for_singly_linked_list.") | |
def is_return_linked_list(str_question: str) -> bool: | |
import operator | |
answer = \ | |
operator.contains(str_question, "_->_list_node") or \ | |
operator.contains(str_question, "_->_optional[_list_node]") | |
return answer | |
def convert_to_linkedlist(numbers: list) -> ListNode: | |
fixed = ListNode() | |
dynamic = fixed | |
for number in numbers: | |
temp = ListNode(number) | |
dynamic.next = temp | |
dynamic = dynamic.next | |
return fixed.next | |
def convert_to_list(head: ListNode) -> list: | |
result = [] | |
while head: | |
result.append(head.val) | |
head = head.next | |
return result | |
def load_args(args_name_and_value: list, linked_list: bool) -> dict: | |
import json | |
loaded_args = {} | |
for arg_name_and_value in args_name_and_value: | |
arg_name, arg_value = arg_name_and_value.replace(' ', '').split("=") | |
arg_value = json.loads(arg_value) | |
if linked_list and type(arg_value) == list: | |
arg_value = convert_to_linkedlist(arg_value) | |
loaded_args[arg_name] = arg_value | |
return loaded_args | |
def get_test_cases(str_examples: str, linked_list: bool): | |
import re | |
import json | |
examples = re.split('Example.+?:', str_examples)[1:] | |
all_input, all_output = [], [] | |
for example in examples: | |
input_flag = 0 | |
output_flag = 0 | |
lines = example.split('\n') | |
for line in lines: | |
line = line.strip() | |
line = line.lstrip('# ') | |
if line.startswith('Input: '): | |
args_name_and_value = line.replace('Input: ', '').split(', ') | |
input_flag += 1 | |
loaded_args = load_args(args_name_and_value, linked_list) | |
all_input.append(loaded_args) | |
continue | |
if line.startswith('Output: '): | |
output = json.loads(line.replace('Output: ', '')) | |
output_flag += 1 | |
all_output.append(output) | |
if input_flag != 1 or output_flag != 1: | |
raise RuntimeError('Example extraction error. Please checke "question_content"') | |
return zip(all_input, all_output) | |
def auto_test(input_and_output: zip, linked_list: bool): | |
function = getattr(Solution(), dir(Solution())[-1]) | |
wrong_answer_cunter = 0 | |
for input_, output in input_and_output: | |
your_answer = function(**input_) | |
if linked_list or type(your_answer) == ListNode: | |
your_answer = convert_to_list(your_answer) | |
for arg_name, arg_value in input_.items(): | |
if type(arg_value) == ListNode: | |
input_[arg_name] = convert_to_list(arg_value) | |
message = f"Input: {input_}, Output: {output}\nYour answer: \033[31m{your_answer}\033[0m" | |
try: | |
assert your_answer == output, message | |
except AssertionError as e: | |
wrong_answer_cunter += 1 | |
print("\n", e) | |
if wrong_answer_cunter > 0: | |
return '\033[31mWrong Answer\033[0m' | |
else: | |
return '\033[32mAccepted\033[0m' | |
# endregion auto_test | |
if __name__ == '__main__': | |
input_linked_list = is_input_linked_list(question_code) | |
return_linked_list = is_return_linked_list(question_code) | |
test_cases = get_test_cases(question_content, input_linked_list) | |
print('\n', auto_test(test_cases, return_linked_list)) |
第23题无法自动输入测试用例
第26题无法自动输入测试用例
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
暂未适配 LeetCode.cn 站点