-
Notifications
You must be signed in to change notification settings - Fork 0
/
_LeetCodeInit_.py
executable file
·115 lines (97 loc) · 3.66 KB
/
_LeetCodeInit_.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python3
############################################################
# Script to build a problem directory with this structure: #
# #
# . [problem_name] #
# ├── README.md #
# ├── [problem_name].py #
# └── others #
# ├── .gitignore #
# └── ... #
# #
############################################################
import os
from typing import *
from colorama import Fore, Style
# Print error msg
def inputError():
print('Write "' +
Fore.GREEN + '1' +
Style.RESET_ALL+'", "' +
Fore.LIGHTYELLOW_EX + '2' +
Style.RESET_ALL+'" or "' +
Fore.RED + '3' +
Style.RESET_ALL+'", please.')
# Aks for problem difficulty and return it as a string
def getDifficulty():
try:
difficulty: int = int(input("Which difficulty is the problem rated as?\n" +
Fore.GREEN + " - Easy -> 1\n" +
Fore.LIGHTYELLOW_EX + " - Medium -> 2\n" +
Fore.RED + " - Hard -> 3\n"+
Style.RESET_ALL + "--> "))
if (difficulty < 1 or difficulty > 3):
raise ValueError
except ValueError:
inputError()
exit(-1)
if difficulty == 1:
return "Easy"
elif difficulty == 2:
return "Medium"
else:
return "Hard"
# Remove first space in a string
def removeFirstSpace(name: str):
spacePos: int = name.find(' ')
if spacePos != -1 and name[spacePos - 1] == ".":
return name[:spacePos] + name[spacePos + 1:]
else:
return name
# Get problem number and name
def getName():
name: str = input('\nPlease write the number as well as the name of the problem.\n - Ex: "1. Two Sum"\n--> ')
name = removeFirstSpace(name)
return name
# Creates directories
def createDirStructure(finalPath: str):
# Create folder if the problem doesn't exist
if os.path.isdir(finalPath) == False:
os.mkdir(finalPath)
finalPath = finalPath + "/others"
os.mkdir(finalPath)
return True
return False
# Creates and fills files
def createFiles(finalPath: str, name: str):
readmePath: str = finalPath + "/README.md"
readmeContent: str = "Change for Clip LeetCode Markdown"
pyPath: str = finalPath + "/" + name[name.find(".") + 1:] + ".py"
pyContent: str = "#!/usr/bin/env python3\nfrom typing import *\n\n#* All tests passed --- x/x passed\n"
gitignorePath: str = finalPath + "/others/.gitignore"
gitignoreContent: str = "../.vscode/\n"
# Create readme
with open(readmePath, 'w') as _readme:
_readme.write(readmeContent)
# Create .py + give permissions
with open(pyPath, 'w') as _pyPath:
_pyPath.write(pyContent)
os.chmod(pyPath, 0o755)
# Create .gitignore
with open(gitignorePath, 'w') as _gitignorePath:
_gitignorePath.write(gitignoreContent)
# Build structure
def createStructure(difficulty: str, name: str):
currDir = os.getcwd()
name = name.replace(" ", "_")
finalPath: str = "/".join([currDir, difficulty, name])
successDirs = createDirStructure(finalPath)
if successDirs == True:
createFiles(finalPath, name)
def main():
difficulty: str = getDifficulty()
name: str = getName()
createStructure(difficulty, name)
print("\nInitialization finished!")
if __name__ == "__main__":
main()