-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate.py
100 lines (75 loc) · 3.54 KB
/
create.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
import sys
import os
import glob
def getCategoryPaths(categoryNumber: str) -> str:
return glob.glob(f'{categoryNumber.zfill(2)}-*')
def getSolutionPaths(leetcodeID: str) -> list[str]:
return \
glob.glob(f'./**/{leetcodeID.zfill(5)}-*/', recursive=True) + \
glob.glob(f'./**/{leetcodeID.zfill(5)}-*/{leetcodeID.zfill(5)}-*.*', recursive=True)
def getProblem(leetcodeURL: str) -> str:
return ' '.join(leetcodeURL.strip('/').split('/')[-1].split('-')).title()
def createCategoryDirectory(categoryNumber: str) -> str:
newCategoryName = input(f"{categoryNumber.zfill(2)}-")
fp = f"./{categoryNumber.zfill(2)}-{newCategoryName}/"
os.makedirs(fp)
print(f'Category directory created: {fp}')
return fp
def createProblemDirectory(cd: str, leetcodeID: str, leetcodeURL: str) -> str:
newProblemName = f"{leetcodeID.zfill(5)}-{getProblem(leetcodeURL).replace(' ', '_')}"
fp = f"{cd}/{newProblemName}/"
os.makedirs(fp)
print(f"Solution directory created: {fp}")
return fp
def createSolutionFiles(sd: str, leetcodeID: str, leetcodeURL: str):
fileTypes = ['cpp', 'java', 'go', 'py']
filename = f"{leetcodeID.zfill(5)}-{getProblem(leetcodeURL).replace(' ', '_').lower()}"
commentSymbol = '//'
for filetype in fileTypes:
if filetype == 'py': commentSymbol = '#'
comment = f'{commentSymbol} {leetcodeID}: {getProblem(leetcodeURL)}\n{commentSymbol} {leetcodeURL}\n\n'
with open(f'{sd}/{filename}.{filetype}', 'w+') as f:
f.writelines([comment])
def addEntry(leetcodeID, leetcodeURL):
paths = sorted(getSolutionPaths(leetcodeID)[1:])
mdPaths = []
for path in paths:
extension = path.split('.')[-1]
language = ''
if extension == 'cpp': language = 'C++'
elif extension == 'java': language = 'Java'
elif extension == 'go': language = 'Go'
elif extension == 'py': language = 'Python'
mdPaths.append(f'[{language}]({path})')
pathsString = ' • '.join(mdPaths)
entry = f"| {leetcodeID} | | [{getProblem(leetcodeURL)}]({leetcodeURL}) | {pathsString} |\n"
print(entry)
if __name__ == '__main__':
if len(sys.argv) < 4 :
print("Insufficient arguements passed ! Usage :")
print("python creator.py <category-number> <leetcode-id> <leetcode-url>")
exit(0)
categoryNumber, leetcodeID, leetcodeURL = sys.argv[1], sys.argv[2], sys.argv[3]
categoryPaths = getCategoryPaths(categoryNumber)
solutionPaths = getSolutionPaths(leetcodeID)
categoryDirectoryPath = ''
solutionDirectoryPath = ''
# check & create solutions directory
if len(solutionPaths) > 0:
print(f'The problem with LeetCode ID : {leetcodeID} is already present.')
for s in solutionPaths: print("\t- ", s)
else:
if len(categoryPaths) > 0 :
print(f"Category {categoryNumber} directory already exists.")
for c in categoryPaths: print("\t- ", c)
else:
print(f"Category {categoryNumber} directory does not exists. Create category directory : ", end='')
# create category directory
createCategoryDirectory(categoryNumber)
# create solutions directory
categoryDirectoryPath = getCategoryPaths(categoryNumber)[0]
solutionDirectoryPath = createProblemDirectory(categoryDirectoryPath, leetcodeID, leetcodeURL)
# create solutions files
createSolutionFiles(solutionDirectoryPath, leetcodeID, leetcodeURL)
# add entry to README.md
addEntry(leetcodeID, leetcodeURL)