/
mkprob.py
157 lines (126 loc) · 4.01 KB
/
mkprob.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
This Script Creates Problem Directory along with
it's Solutions.md containing Link to Solution of that particular
problem in different languages.
"""
from os import mkdir
from os.path import join, isdir, isfile
from argparse import ArgumentParser, RawTextHelpFormatter
from textwrap import dedent
def border(func):
def wrap(*args):
print('='*50)
print('\n')
func(*args)
print('\n')
print('='*50)
return wrap
@border
def create_dir(dir_path: str, sol_path: str, number: int) -> None:
"""
This function creates Problem directory with
Problem Number as suffix
"""
mkdir(dir_path)
print(f'Problem Directory "{dir_path}" Created!')
create_solution(dir_path, sol_path, number)
@border
def create_markdown(path: str, number: int) -> None:
"""
This function creates Solutions.md in Problem
Directory
"""
markdown = f"# Problem #{number}\n\n\n"
markdown_content = """
### Solutions
+ [Python](solution.py)
+ [Javascript](solution.js)
+ [C++](solution.cpp)
### Solutions [Optimized] [TODO]
+ [Python](solution_optimized.py)
+ [Javascript](solution_optimized.js)
+ [C++](solution_optimized.cpp)
"""
if not isfile(path):
with open(path, 'w') as f:
f.write(markdown)
f.write(markdown_content)
print('"Solutions.md" Created!')
else:
print('"Solutions.md" Already Exists!')
@border
def create_solution(dir_path: str, sol_path: str, number: int) -> None:
"""
This function creates Solution boilerplate
consisting of Program files and then calls
create_markdown() for Creating Solution
Markdown file for Navigating each Solution
with ease
"""
extensions = ['js', 'py', 'cpp']
for ext in extensions:
# Program file name with Path
scr_name = join(dir_path, f'solution.{ext}')
scr_name2 = join(dir_path, f'solution_optimized.{ext}')
if isfile(scr_name):
# If file exists, then don't touch it
print(f'"solution.{ext}" Already Exists!')
else:
# Else Create File
open(scr_name, 'w').close()
print(f'"solution.{ext}" Created!')
if isfile(scr_name2):
# If file exists, then don't touch it
print(f'"solution_optimized.{ext}" Already Exists!')
else:
open(scr_name2, 'w').close()
print(f'"solution_optimized.{ext}" Created!')
create_markdown(sol_path, number)
print(f'Solution Boilerplate for Problem {number} Created!')
# Creating Argument Parser object
parser = ArgumentParser(
description=dedent('''
Creates Directory or Directories with Problem Number(s) as Suffix and adds
Solutions Boilerplate consisting of Solutions.md, solution.js, solution.py and
solution.cpp.
'''),
usage="%(prog)s [options] problem_number",
prog='mkprob.py',
formatter_class=RawTextHelpFormatter,
epilog=dedent("""
Example Usage:
Usage #1: Creating One Problem Directory
mkprob.py 17
Usage #2: Creating Multiple Problem Directories
mkprob.py 17 18 19 20
""")
)
# Version Number of the Script
parser.version = '0.1'
# Positional Argument for Problem Number
parser.add_argument(
'problem_number',
metavar='problem_number',
type=int,
help='Creates Directory or Directories with Problem Number(s) as suffix',
action='store', nargs='+'
)
# Optional Agument for Version Number
parser.add_argument('-v', '--version', action='version')
# Parse Aruguments
args = parser.parse_args()
numbers = list(set(args.problem_number))
for number in numbers:
# File Names
dir_name = f'Problem {number}'
sol_file_name = 'Solutions.md'
# Path to Solutions.md
sol_path = join(dir_name, sol_file_name)
prompt = True
if isdir(dir_name):
print(f'\nDirectory "{dir_name}" Already Exists...')
create_solution(dir_name, sol_path, number)
else:
print("\n")
create_dir(dir_name, sol_path, number)
print('\nExiting..')