-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_badges.py
More file actions
111 lines (85 loc) · 3.51 KB
/
update_badges.py
File metadata and controls
111 lines (85 loc) · 3.51 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
#!/usr/bin/env python3
"""
Script to automatically update README badges with current problem counts.
This script counts the number of solved problems in each language and updates the README.md file.
"""
import os
import re
from pathlib import Path
def count_cpp_problems():
"""Count C++ problems in the problems directory."""
problems_dir = Path("problems")
if not problems_dir.exists():
return 0
cpp_count = 0
for problem_dir in problems_dir.iterdir():
if problem_dir.is_dir():
# Check if this problem has C++ files
cpp_files = list(problem_dir.glob("*.cc")) + list(problem_dir.glob("*.cpp"))
if cpp_files:
cpp_count += 1
return cpp_count
def count_python_problems():
"""Count Python problems in the problems directory."""
problems_dir = Path("problems")
if not problems_dir.exists():
return 0
python_count = 0
for problem_dir in problems_dir.iterdir():
if problem_dir.is_dir():
# Check if this problem has Python files
python_files = list(problem_dir.glob("*.py"))
if python_files:
python_count += 1
return python_count
def update_readme_badges(cpp_count, python_count):
"""Update the README.md file with new badge counts."""
readme_path = Path("README.md")
if not readme_path.exists():
print("ERROR: README.md not found!")
return False
# Read the current README content
with open(readme_path, "r", encoding="utf-8") as f:
content = f.read()
# Define the new badge lines
cpp_badge = f"[](https://github.com/mathusanm6/LeetCode/tree/main/problems)"
python_badge = f"[](https://github.com/mathusanm6/LeetCode/tree/main/problems)"
# Update C++ badge
cpp_pattern = r"\[\!\[C\+\+ Solutions\].*?\)\]\(.*?\)"
if re.search(cpp_pattern, content):
content = re.sub(cpp_pattern, cpp_badge, content)
else:
print("WARNING: C++ badge pattern not found in README.md")
# Update Python badge
python_pattern = r"\[\!\[Python Solutions\].*?\)\]\(.*?\)"
if re.search(python_pattern, content):
content = re.sub(python_pattern, python_badge, content)
else:
print("WARNING: Python badge pattern not found in README.md")
# Write the updated content back
with open(readme_path, "w", encoding="utf-8") as f:
f.write(content)
return True
def main():
"""Main function to count problems and update badges."""
print("🔍 Counting solved problems...")
# Count problems in each language
cpp_count = count_cpp_problems()
python_count = count_python_problems()
print(f"📊 Found {cpp_count} C++ problems")
print(f"📊 Found {python_count} Python problems")
# Update the README
if update_readme_badges(cpp_count, python_count):
print("✅ README.md badges updated successfully!")
print(f" C++ Solutions: {cpp_count}")
print(f" Python Solutions: {python_count}")
else:
print("❌ Failed to update README.md")
return 1
return 0
if __name__ == "__main__":
# Change to the repository root directory
script_dir = Path(__file__).parent
repo_root = script_dir.parent
os.chdir(repo_root)
exit(main())