-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_leetcode_problem.sh
More file actions
executable file
·49 lines (43 loc) · 1.09 KB
/
add_leetcode_problem.sh
File metadata and controls
executable file
·49 lines (43 loc) · 1.09 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
#!/bin/bash
# Check for correct argument count
if [ "$#" -lt 4 ]; then
echo "Usage: $0 <difficulty: easy|medium|hard> <number> <name_with_underscores> <extension: py|cpp|java ...>"
exit 1
fi
# Parse arguments
difficulty=$1
number=$(printf "%04d" "$2")
name_snake=$3
# Validate difficulty
case "$difficulty" in
easy|medium|hard) ;;
*)
echo "Invalid difficulty. Use: easy, medium, or hard."
exit 1
;;
esac
# Validate name: only lowercase letters, underscores, and numbers
if [[ ! "$name_snake" =~ ^[a-z0-9_]+$ ]]; then
echo "Invalid name. Use only lowercase letters, numbers, and underscores."
exit 1
fi
# Compose the problem folder name and create it
target_dir="${difficulty}/${number}_${name_snake}"
mkdir -p "$target_dir"
# Shift arguments by 3
shift 3
for ext in "$@"; do
case "$ext" in
java)
touch "${target_dir}/Solution.java"
echo "Added ${target_dir}/Solution.java"
;;
py|cpp)
touch "${target_dir}/solution.${ext}"
echo "Added ${target_dir}/solution.${ext}"
;;
*)
echo "Unsupported extension: $ext"
;;
esac
done