-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm
More file actions
executable file
·199 lines (178 loc) · 3.89 KB
/
algorithm
File metadata and controls
executable file
·199 lines (178 loc) · 3.89 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/bin/bash
MODE="unknown"
LANG="unknown"
EXT=""
PLATFORM=""
PROBLEM_NUMBER=""
PROBLEM_NAME=""
PROBLEM_URL=""
DATE=$(date +%F)
function LANGUAGE_MAPPING {
case $1 in
c++|cpp|cc)
EXT="cpp"
;;
go)
EXT="go"
;;
javascript|js)
EXT="js"
;;
python|py)
EXT="py"
;;
esac
}
function PLATFORM_MAPPING {
case $1 in
b|baekjoon)
PLATFORM="baekjoon"
;;
p|programmers)
PLATFORM="programmers"
;;
l|leetcode)
PLATFORM="leetcode"
;;
esac
}
function printHelp {
echo "Usage: algorithm [command] [platform] [options]"
echo ""
echo " Commands: create, show, commit, help"
echo ""
echo " Platforms: baekjoon, leetcode, programmers, template"
echo ""
echo " Options:"
echo -e " -l, --lang, --language Specify target language"
echo -e " -h, --help \t\t Show help message"
echo ""
echo " options for create:"
echo -e " -no, --number \t Specify problem number"
echo -e " -n, --name \t\t Specify problem name"
echo -e " -u, --url \t\t Specify problem url"
echo -e " -d, --date \t\t Specify file creation date (default: today's date)"
}
function printConfig {
echo "MODE=${MODE}"
echo "LANG=${LANG}"
echo "EXT=${EXT}"
echo "PLATFORM=${PLATFORM}"
echo "PROBLEM_NUMBER=${PROBLEM_NUMBER}"
echo "PROBLEM_NAME=${PROBLEM_NAME}"
echo "PROBLEM_URL=${PROBLEM_URL}"
echo "DATE=${DATE}"
echo ""
}
function checkLangSupport {
lang=$LANG
LANGUAGE_MAPPING $lang
if [ -z $EXT ]; then
echo "Error: Language ${lang} is not supported!"
exit 1
fi
}
function createFile {
while [[ $# -gt 0 ]]; do
case $1 in
-no|--number)
PROBLEM_NUMBER=$2
shift 2
;;
-n|--name)
PROBLEM_NAME=$2
shift 2
;;
-u|--url)
PROBLEM_URL=$2
shift 2
;;
-d|--date)
DATE=$2
shift 2
;;
*)
echo "Error: unknown options ${1}"
echo ""
printHelp
exit 1
;;
esac
done
printConfig
scriptPath=$(dirname $0)
platformPath="${scriptPath}/${PLATFORM}"
templateDir="${scriptPath}/templates"
templateFilename="${PLATFORM}.${EXT}"
outFilePath="${platformPath}/${PROBLEM_NUMBER}. ${PROBLEM_NAME}/solution.${EXT}"
if [ ! -d $templateDir ]; then
echo "Error: Directory for templates is not exist."
exit 1
fi
if [ ! -d $platformPath ]; then
echo "Info: Directory for platform $PLATFORM is not exist. Creating $platformPath"
mkdir -p $platformPath
fi
if [ ! -f "${templateDir}/${templateFilename}" ]; then
echo "Error: Template does not exist for platform $PLATFORM language $LANG in $templateFilename"
exit 1
fi
mkdir -p "${platformPath}/${PROBLEM_NUMBER}. ${PROBLEM_NAME}"
sed -e "s|number:|number: ${PROBLEM_NUMBER}|" \
-e "s|name:|name: ${PROBLEM_NAME}|" \
-e "s|https://leetcode.com/problems/|${PROBLEM_URL}|" \
"${templateDir}/${templateFilename}" > "${outFilePath}"
echo "Successfully created solution file at ${outFilePath}"
}
function showAlgorithm {
echo "Warning: show command not implemented yet."
}
if [[ $1 = "create" ]]; then
MODE="create"
elif [[ $1 = "show" ]]; then
MODE="show"
elif [ $1 = "help" ] || [ $1 = "-h" ]; then
printHelp
exit 0
else
echo "Error: Unknown command $1"
echo ""
printHelp
exit 1
fi
shift
if [ ! -z $1 ]; then
PLATFORM_MAPPING $1
if [ -z $PLATFORM ]; then
echo "Error: Platform $1 is not supported!"
echo ""
printHelp
fi
fi
shift
# Parse options for command
while [[ $# -gt 0 ]]; do
parseDone=false
case $1 in
-l|--lang|--language)
LANG=$2
checkLangSupport
shift 2
;;
-h|--help)
printHelp
shift
;;
*)
parseDone=true
;;
esac
if [ $parseDone ]; then
break
fi
done
if [ $MODE = "create" ]; then
createFile "$@"
elif [ $MODE = "show" ]; then
showAlgorithm "$@"
fi