4
4
from .base import BaseLanguageInterface
5
5
6
6
FUNCTION_SIGNATURE_PATTERN = re .compile (
7
- r"^def (?P<name>\w+)\((?P<params>[\w\s,= ]*)\) -> (?P<returnType>\w +):$" ,
7
+ r"^(class Solution:\n)?\s* def (?P<name>\w+)\((?P<params>[^) ]*)\) -> (?P<returnType>[^:] +):$" ,
8
8
flags = re .MULTILINE ,
9
9
)
10
10
11
+ # LeetCode uses Typing types, not Python 3.9+ types
12
+ TYPING_IMPORT_TEMPLATE = "from typing import *\n \n "
13
+
11
14
TEST_FILE_TEMPLATE = """\
15
+ from solution import Solution
16
+
17
+
12
18
if __name__ == "__main__":
13
19
{params_setup}
14
- result = {name}({params_call})
20
+ result = Solution(). {name}({params_call})
15
21
print("result:", result)
16
22
"""
17
23
24
+
18
25
class Python3LanguageInterface (BaseLanguageInterface ):
19
26
"""Implementation of the Python 3 language project template interface."""
20
27
@@ -24,16 +31,25 @@ def write_project_files(self, template: str):
24
31
"""Creates the project template for Python 3."""
25
32
26
33
with open ("solution.py" , "w" , encoding = "utf-8" ) as file :
27
- file .write (template + "\n " )
28
-
29
- params = self .groups ["params" ].split (", " ) if self .groups ["params" ] else []
34
+ file .write (f"{ TYPING_IMPORT_TEMPLATE } \n { template } pass\n " )
35
+
36
+ params = (
37
+ [
38
+ param
39
+ for param in self .groups ["params" ].split (", " )
40
+ if param and param != "self"
41
+ ]
42
+ if self .groups ["params" ]
43
+ else []
44
+ )
30
45
self .groups ["params_setup" ] = "\n " .join (
31
- f"{ param .split ('=' )[0 ]} = 0" for param in params if param
46
+ param if "=" in param else f"{ param } = None" for param in params
47
+ )
48
+ self .groups ["params_call" ] = ", " .join (
49
+ param .split ("=" )[0 ].split (":" )[0 ].strip () for param in params
32
50
)
33
- self .groups ["params_call" ] = ", " .join (param .split ('=' )[0 ] for param in params )
34
51
35
52
formatted = TEST_FILE_TEMPLATE .format (** self .groups )
36
53
37
54
with open ("test.py" , "w" , encoding = "utf-8" ) as file :
38
- file .write (formatted )
39
-
55
+ file .write (f"{ TYPING_IMPORT_TEMPLATE } { formatted } " )
0 commit comments