-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.py
75 lines (63 loc) · 1.66 KB
/
codegen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import argparse
import os
CURWCPATH = os.path.dirname(__file__)
def generate_cpp_template(filename, num):
template_code = """\
/**
The problem description goes here.
source from: leetcode {}.
*/
#include "algobase.h"
using namespace std;
// Linux-specific optimizations with GCC or Clang
#if defined(__linux__)
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC optimize("Ofast")
static auto __optimize = []() {{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}}();
#endif
#endif
// macOS-specific optimizations with Clang
#if defined(__APPLE__) && defined(__MACH__)
#if defined(__clang__)
#pragma clang optimize on
static auto __optimize = []() {{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}}();
#endif
#endif
// Windows-specific optimizations with MSVC
#if defined(_WIN32)
#if defined(_MSC_VER)
#include <cstdlib>
#pragma optimize("t", on)
static auto __optimize = []() {{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}}();
#endif
#endif
int main() {{
// Your code here
return 0;
}}
""".format(num)
with open(os.path.join(CURWCPATH, filename), "w") as f:
f.write(template_code)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="codegen")
parser.add_argument(
"--name", type=str, help="cpp file name", default="template.cpp"
)
parser.add_argument(
"--num", type=int, help="leetcode question number", default="-1"
)
args = parser.parse_args()
generate_cpp_template(args.name, args.num)
print(f"C++ template code has been generated in {args.name}, which is from leetcode {args.num}")