|
| 1 | +import os |
| 2 | +import re |
| 3 | + |
| 4 | +def parse_solution_files(): |
| 5 | + solution_files = [] |
| 6 | + for root, _, files in os.walk("."): |
| 7 | + for file in files: |
| 8 | + if file.endswith(".cs"): |
| 9 | + solution_files.append(os.path.join(root, file)) |
| 10 | + return solution_files |
| 11 | + |
| 12 | +def extract_problem_info(file_path): |
| 13 | + with open(file_path, "r") as file: |
| 14 | + content = file.read() |
| 15 | + match = re.search(r"https://leetcode.com/problems/([a-zA-Z0-9\-]+)/", content) |
| 16 | + if match: |
| 17 | + url = match.group(0) |
| 18 | + title = match.group(1).replace("-", " ").title() |
| 19 | + return title, url |
| 20 | + return None, None |
| 21 | + |
| 22 | +def generate_solved_problems_list(solution_files): |
| 23 | + solved_problems = [] |
| 24 | + for file_path in solution_files: |
| 25 | + title, url = extract_problem_info(file_path) |
| 26 | + if title and url: |
| 27 | + solved_problems.append(f"- [{title}]({url})") |
| 28 | + return solved_problems |
| 29 | + |
| 30 | +def update_readme(solved_problems): |
| 31 | + with open("README.md", "r") as file: |
| 32 | + readme_content = file.read() |
| 33 | + |
| 34 | + solved_problems_section = "## Solved Problems\n\n" + "\n".join(solved_problems) + "\n" |
| 35 | + updated_readme_content = re.sub(r"## Solved Problems\n\n.*", solved_problems_section, readme_content, flags=re.DOTALL) |
| 36 | + |
| 37 | + with open("README.md", "w") as file: |
| 38 | + file.write(updated_readme_content) |
| 39 | + |
| 40 | +if __name__ == "__main__": |
| 41 | + solution_files = parse_solution_files() |
| 42 | + solved_problems = generate_solved_problems_list(solution_files) |
| 43 | + update_readme(solved_problems) |
0 commit comments