@@ -27,6 +27,14 @@ def line_comment_symbol(self) -> str:
2727 @property
2828 def template_code (self ) -> str :
2929 return r"""
30+ from typing import *
31+
32+ class TreeNode:
33+ def __init__(self, x):
34+ self.val = x
35+ self.left = None
36+ self.right = None
37+
3038# BEGIN SUBMIT
3139
3240# BEGIN USER TEMPLATE
@@ -43,14 +51,6 @@ def template_code(self) -> str:
4351
4452# END STATEMENT
4553
46- from typing import List, Optional
47-
48- class TreeNode:
49- def __init__(self, x):
50- self.val = x
51- self.left = None
52- self.right = None
53-
5454
5555def _construct_tree(parent: List[Optional[int]], idx: int = 0) -> Optional[TreeNode]:
5656 if idx >= len(parent) or parent[idx] is None:
@@ -79,11 +79,11 @@ def test(msg: str, a, b):
7979 "string" : "str" ,
8080 }
8181
82- def _convert_cpp_type_to_py (self , type_name : str ) -> str :
82+ def _convert_cpp_type (self , type_name : str ) -> str :
8383 type_name = type_name .strip ().rstrip ("*&" )
8484 if type_name .startswith ("vector<" ) and type_name .endswith (">" ):
8585 inner_type_name = type_name [len ("vector<" ):- len (">" )]
86- return f"List[{ self ._convert_cpp_type_to_py (inner_type_name )} ]"
86+ return f"List[{ self ._convert_cpp_type (inner_type_name )} ]"
8787 return self .TYPE_MAP .get (type_name , type_name )
8888
8989 def generate_solution_code (self , signature : Signature ) -> Code :
@@ -95,15 +95,15 @@ def generate_solution_code(self, signature: Signature) -> Code:
9595 functions = [signature .function ]
9696 fn_codes = []
9797 for func_sig in functions :
98- args = ", " .join (f"{ arg_name } : { self ._convert_cpp_type_to_py (arg_type )} "
98+ args = ", " .join (f"{ arg_name } : { self ._convert_cpp_type (arg_type )} "
9999 for arg_type , arg_name in func_sig .arguments )
100100 if func_sig .name == class_name :
101101 fn_code = [
102102 f" def __init__(self, { args } ):" ,
103103 f" pass" ]
104104 else :
105105 fn_code = [
106- f" def { func_sig .name } (self, { args } ) -> { self ._convert_cpp_type_to_py (func_sig .return_type )} :" ,
106+ f" def { func_sig .name } (self, { args } ) -> { self ._convert_cpp_type (func_sig .return_type )} :" ,
107107 f" pass" ]
108108 fn_codes .append (fn_code )
109109 code = [f"class { class_name } :" ] + self .list_join (fn_codes , ["" ])
@@ -128,7 +128,7 @@ def to_tree(parent: List[Optional[int]]) -> str:
128128 return f"_construct_tree([{ ', ' .join ('None' if x is None else str (x ) for x in parent )} ])"
129129
130130 def to_val (val : Any , type_name : str ) -> str :
131- if type_name == "TreeNode" :
131+ if self . _convert_cpp_type ( type_name ) == "TreeNode" :
132132 return to_tree (val )
133133 return to_str (val )
134134
0 commit comments