Remove hardcoded difficulties and add dynamic difficulty management system

This commit is contained in:
Carlos
2025-07-11 12:29:14 -04:00
parent ce9f0dcf97
commit e1a096c5bc
3 changed files with 223 additions and 22 deletions

View File

@@ -109,6 +109,26 @@ This repo tracks my journey solving LeetCode problems — not just code, but my
## 🛠️ Tools & Scripts
- `create_missing_notes.sh`: Automatically creates missing note files for solved problems
- `update_difficulties.sh`: Manage problem difficulties dynamically without hardcoding
### Script Usage Examples:
```bash
# Create missing notes for new problems
./create_missing_notes.sh
# Add difficulty mapping for a problem
./update_difficulties.sh -a 1 Easy
./update_difficulties.sh -a 4 Hard
# List all difficulty mappings
./update_difficulties.sh -l
# Update all note files with correct difficulties
./update_difficulties.sh -u
# Remove difficulty mapping
./update_difficulties.sh -r 1
```
## 📚 Resources
- [LeetCode Problem Set](https://leetcode.com/problemset/)

View File

@@ -37,26 +37,6 @@ filename_to_title() {
echo "$title" | sed 's/-/ /g' | sed 's/\b\w/\U&/g' | sed 's/\bii\b/II/g' | sed 's/\biii\b/III/g'
}
# Function to determine difficulty based on problem number ranges
get_difficulty() {
local problem_num=$1
# Hard problems (known)
case $problem_num in
4|10|42|135|149|248)
echo "Hard"
;;
# Easy problems (known)
1|9|13|14|20|21|28|35|66|69|88|121|125|141|169|202|205|206|217|219|228|242|243|246|290|383|392|704|1200|2053|2441|3442|3445)
echo "Easy"
;;
# Everything else is Medium
*)
echo "Medium"
;;
esac
}
# Function to get difficulty color for shields
get_difficulty_color() {
local difficulty=$1
@@ -76,6 +56,15 @@ get_difficulty_color() {
esac
}
# Function to determine difficulty dynamically (default to Medium for unknown problems)
get_difficulty() {
local problem_num=$1
# For now, default to Medium for all problems
# This can be enhanced later with API calls or a separate difficulty mapping file
echo "Medium"
}
# Function to create note file
create_note_file() {
local problem_num=$1
@@ -157,7 +146,7 @@ if [ -d "$EXERCISES_DIR" ]; then
# Convert filename to title
title=$(filename_to_title "$filename")
# Get difficulty
# Get difficulty (default to Medium for now)
difficulty=$(get_difficulty "$problem_num")
# Create note file
@@ -180,4 +169,5 @@ echo "✅ New notes created: $created_count"
echo "⏭️ Notes skipped (already exist): $skipped_count"
echo "📁 Notes directory: $NOTES_DIR"
echo ""
echo "💡 Tip: Run this script whenever you solve new problems to automatically create note templates."
echo "💡 Tip: Run this script whenever you solve new problems to automatically create note templates."
echo "🔧 Note: All problems default to 'Medium' difficulty. You can manually update difficulties in the notes."

191
update_difficulties.sh Executable file
View File

@@ -0,0 +1,191 @@
#!/bin/bash
# LeetCode Difficulty Updater
# This script helps manage problem difficulties dynamically
NOTES_DIR="src/notes"
DIFFICULTY_FILE=".difficulty_mapping"
# Function to show usage
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -a, --add <problem_number> <difficulty> Add/update difficulty for a problem"
echo " -r, --remove <problem_number> Remove difficulty mapping for a problem"
echo " -l, --list List all difficulty mappings"
echo " -u, --update-notes Update all note files with correct difficulties"
echo " -h, --help Show this help message"
echo ""
echo "Difficulties: Easy, Medium, Hard"
echo ""
echo "Examples:"
echo " $0 -a 1 Easy"
echo " $0 -a 4 Hard"
echo " $0 -r 1"
echo " $0 -l"
echo " $0 -u"
}
# Function to get difficulty color for shields
get_difficulty_color() {
local difficulty=$1
case $difficulty in
"Easy")
echo "brightgreen"
;;
"Medium")
echo "orange"
;;
"Hard")
echo "red"
;;
*)
echo "blue"
;;
esac
}
# Function to add/update difficulty mapping
add_difficulty() {
local problem_num=$1
local difficulty=$2
# Validate difficulty
case $difficulty in
"Easy"|"Medium"|"Hard")
;;
*)
echo "❌ Error: Invalid difficulty '$difficulty'. Use Easy, Medium, or Hard."
exit 1
;;
esac
# Create difficulty file if it doesn't exist
touch "$DIFFICULTY_FILE"
# Remove existing entry if it exists
sed -i.bak "/^$problem_num:/d" "$DIFFICULTY_FILE"
# Add new entry
echo "$problem_num:$difficulty" >> "$DIFFICULTY_FILE"
echo "✅ Added difficulty mapping: Problem $problem_num -> $difficulty"
}
# Function to remove difficulty mapping
remove_difficulty() {
local problem_num=$1
if [ -f "$DIFFICULTY_FILE" ]; then
sed -i.bak "/^$problem_num:/d" "$DIFFICULTY_FILE"
echo "✅ Removed difficulty mapping for problem $problem_num"
else
echo "⚠️ No difficulty mapping file found"
fi
}
# Function to list all difficulty mappings
list_difficulties() {
if [ -f "$DIFFICULTY_FILE" ] && [ -s "$DIFFICULTY_FILE" ]; then
echo "📋 Current Difficulty Mappings:"
echo "Problem | Difficulty"
echo "--------|-----------"
sort -n "$DIFFICULTY_FILE" | while IFS=':' read -r problem difficulty; do
printf "%-7s | %s\n" "$problem" "$difficulty"
done
else
echo "📋 No difficulty mappings found"
fi
}
# Function to get difficulty from mapping file
get_difficulty_from_file() {
local problem_num=$1
if [ -f "$DIFFICULTY_FILE" ]; then
local difficulty=$(grep "^$problem_num:" "$DIFFICULTY_FILE" | cut -d':' -f2)
if [ -n "$difficulty" ]; then
echo "$difficulty"
return 0
fi
fi
# Default to Medium if no mapping found
echo "Medium"
return 1
}
# Function to update note files with correct difficulties
update_notes() {
echo "🔄 Updating note files with correct difficulties..."
local updated_count=0
if [ -d "$NOTES_DIR" ]; then
for note_file in "$NOTES_DIR"/*.md; do
if [ -f "$note_file" ]; then
# Extract problem number from filename
local filename=$(basename "$note_file")
local problem_num=$(echo "$filename" | sed 's/^\([0-9]*\)_.*/\1/')
if [ -n "$problem_num" ] && [ "$problem_num" != "$filename" ]; then
# Get correct difficulty
local correct_difficulty=$(get_difficulty_from_file "$problem_num")
local difficulty_color=$(get_difficulty_color "$correct_difficulty")
# Update difficulty in note file
sed -i.bak "s/\[!\[Difficulty\].*\]/\[!\[Difficulty\](https:\/\/img.shields.io\/badge\/Difficulty-$correct_difficulty-$difficulty_color?style=for-the-badge)\]/" "$note_file"
sed -i.bak "s/\*\*Difficulty:\*\* \[.*\]/\*\*Difficulty:\*\* \[$correct_difficulty\](https:\/\/leetcode.com\/problemset\/?difficulty=$(echo "$correct_difficulty" | tr '[:lower:]' '[:upper:]'))/" "$note_file"
echo "✅ Updated: $filename -> $correct_difficulty"
((updated_count++))
fi
fi
done
# Clean up backup files
rm -f "$NOTES_DIR"/*.bak
rm -f "$DIFFICULTY_FILE.bak"
echo "🎉 Updated $updated_count note files"
else
echo "❌ Notes directory not found"
fi
}
# Main script logic
case "${1:-}" in
-a|--add)
if [ $# -eq 3 ]; then
add_difficulty "$2" "$3"
else
echo "❌ Error: Missing arguments for --add"
show_usage
exit 1
fi
;;
-r|--remove)
if [ $# -eq 2 ]; then
remove_difficulty "$2"
else
echo "❌ Error: Missing problem number for --remove"
show_usage
exit 1
fi
;;
-l|--list)
list_difficulties
;;
-u|--update-notes)
update_notes
;;
-h|--help|"")
show_usage
;;
*)
echo "❌ Error: Unknown option '$1'"
show_usage
exit 1
;;
esac