-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_readme.py
More file actions
99 lines (87 loc) · 3.76 KB
/
Copy pathupdate_readme.py
File metadata and controls
99 lines (87 loc) · 3.76 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
import os
import re
README_FILE = "README.md"
SOLUTIONS_FOLDER = "."
def create_progress_bar(percentage):
total_blocks = 20
filled_blocks = int(total_blocks * percentage / 100)
empty_blocks = total_blocks - filled_blocks
bar = f"{'█' * filled_blocks}{'░' * empty_blocks}"
if percentage == 100:
return f"🏆 [{bar}] 100.0% — All problems solved!"
if percentage >= 80:
emoji = "🟩"
elif percentage >= 50:
emoji = "🟨"
else:
emoji = "🟥"
return f"{emoji} [{bar}] {percentage:.1f}%"
def update_readme():
with open(README_FILE, "r", encoding="utf-8") as f:
readme_content = f.read()
solved_files = {file for file in os.listdir(SOLUTIONS_FOLDER) if file.endswith(".py") and file != "update_readme.py"}
# Regex for table row: | num | problem | difficulty | status | solution |
table_pattern = re.compile(
r"(\|\s*(\d+)\s*\|\s*([^\|]+?)\s*\|\s*(Easy|Medium|Hard)\s*\|\s*(✅|❌|⏳)\s*\|\s*([^\|]+?)\s*\|)", re.MULTILINE)
updated_rows = []
solved_count = 0
total_problems = 0
difficulty_stats = {
"Easy": {"total": 0, "solved": 0},
"Medium": {"total": 0, "solved": 0},
"Hard": {"total": 0, "solved": 0},
}
found_rows = table_pattern.findall(readme_content)
for full_row, num, problem, difficulty, status, solution in found_rows:
total_problems += 1
difficulty_stats[difficulty]["total"] += 1
problem_num = int(num)
filename_prefix = f"{problem_num:02}_"
matching_file = next((f for f in solved_files if f.startswith(filename_prefix)), None)
if matching_file:
solved_count += 1
difficulty_stats[difficulty]["solved"] += 1
new_row = f"| {num} | {problem.strip()} | {difficulty} | ✅ | [{matching_file}]({matching_file}) |"
else:
new_row = f"| {num} | {problem.strip()} | {difficulty} | ❌ | - |"
updated_rows.append(new_row)
# Replace old table rows with updated ones
def row_replacer(match):
return updated_rows.pop(0)
updated_content = table_pattern.sub(row_replacer, readme_content)
# Progress bar
percentage = (solved_count / total_problems) * 100 if total_problems > 0 else 0
progress_bar = create_progress_bar(percentage)
progress_pattern = re.compile(r"## 📊 Progress\n.*?\n\n", re.DOTALL)
if "## 📊 Progress" in updated_content:
updated_content = progress_pattern.sub(f"## 📊 Progress\n{progress_bar}\n\n", updated_content)
else:
updated_content = updated_content.replace(
"## 🎯 Goal",
f"## 📊 Progress\n{progress_bar}\n\n## 🎯 Goal"
)
# Summary by difficulty
summary_lines = [
"## 📊 Summary by Difficulty\n",
"| Difficulty | Total | Solved | Unsolved |",
"|------------|-------|--------|----------|",
]
for diff in ["Easy", "Medium", "Hard"]:
total = difficulty_stats[diff]["total"]
solved = difficulty_stats[diff]["solved"]
unsolved = total - solved
summary_lines.append(f"| {diff} | {total} | {solved} | {unsolved} |")
summary_text = "\n".join(summary_lines) + "\n"
summary_pattern = re.compile(r"## 📊 Summary by Difficulty\n(?:\n|.)*?(?=\n##|\Z)", re.DOTALL)
if "## 📊 Summary by Difficulty" in updated_content:
updated_content = summary_pattern.sub(summary_text, updated_content)
else:
updated_content = updated_content.replace(
"## 🎯 Goal",
f"{summary_text}\n## 🎯 Goal"
)
with open(README_FILE, "w", encoding="utf-8") as f:
f.write(updated_content)
print(f"✅ README.md updated! {solved_count}/{total_problems} solved ({percentage:.1f}%)")
if __name__ == "__main__":
update_readme()