|
| 1 | +import os |
| 2 | +import re |
| 3 | +import requests |
| 4 | + |
| 5 | +def extract_problem_info(file_path): |
| 6 | + match = re.match(r"(\d+)-([a-z0-9-]+)\.py", os.path.basename(file_path)) |
| 7 | + if match: |
| 8 | + problem_number, slug = match.groups() |
| 9 | + return int(problem_number), slug, f"[{slug.replace('-', ' ').title()}](https://leetcode.com/problems/{slug}/description/)" |
| 10 | + return None, None, None |
| 11 | + |
| 12 | +def get_problem_difficulty(slug): |
| 13 | + url = "https://leetcode.com/graphql" |
| 14 | + query = """ |
| 15 | + query getQuestionDifficulty($titleSlug: String!) { |
| 16 | + question(titleSlug: $titleSlug) { |
| 17 | + difficulty |
| 18 | + } |
| 19 | + } |
| 20 | + """ |
| 21 | + response = requests.post(url, json={"query": query, "variables": {"titleSlug": slug}}) |
| 22 | + try: |
| 23 | + data = response.json() |
| 24 | + return data["data"]["question"]["difficulty"] |
| 25 | + except (KeyError, TypeError): |
| 26 | + return "Unknown" |
| 27 | + |
| 28 | +def build_readme(): |
| 29 | + topics = [d for d in os.listdir('.') if os.path.isdir(d)] |
| 30 | + problem_entries = [] |
| 31 | + |
| 32 | + for topic in topics: |
| 33 | + for file in os.listdir(topic): |
| 34 | + if file.endswith(".py"): |
| 35 | + problem_number, slug, problem_title = extract_problem_info(file) |
| 36 | + if problem_number: |
| 37 | + difficulty = get_problem_difficulty(slug) |
| 38 | + problem_entries.append((problem_number, problem_title, f"[Python](./{topic}/{file})", topic.replace('_', ' ').title(), difficulty)) |
| 39 | + |
| 40 | + problem_entries.sort() |
| 41 | + |
| 42 | + table_header = "| # | Title | Solution | Type | Difficulty |\n|---| ----- | -------- | ---------- | ---------- |" |
| 43 | + table_rows = [] |
| 44 | + |
| 45 | + for number, title, solution, topic, difficulty in problem_entries: |
| 46 | + table_rows.append(f"|{number:04d}|{title}|{solution}|{topic}| {difficulty} |") |
| 47 | + |
| 48 | + table_content = "\n".join([table_header] + table_rows) |
| 49 | + readme_content = f"""# Coding Everyday Until I Get A Job |
| 50 | +
|
| 51 | +## LeetCode Solutions |
| 52 | +
|
| 53 | +{table_content} |
| 54 | +""" |
| 55 | + |
| 56 | + with open("README.md", "w", encoding="utf-8") as f: |
| 57 | + f.write(readme_content) |
| 58 | + |
| 59 | +if __name__ == "__main__": |
| 60 | + build_readme() |
| 61 | + print("README.md has been updated!") |
0 commit comments