diff --git a/.vscode/settings.json b/.vscode/settings.json index f3caed66..124bd28a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { "editor.formatOnSave": true, - "prettier.tabWidth": 2, + "prettier.tabWidth": 4, "makefile.configureOnOpen": false, "prettier.embeddedLanguageFormatting": "auto", "prettier.bracketSameLine": false, diff --git a/docs/md/0001_two_sum.md b/docs/md/0001_two_sum.md index 976c7158..c1b80838 100644 --- a/docs/md/0001_two_sum.md +++ b/docs/md/0001_two_sum.md @@ -1,9 +1,9 @@ ## 1. Two Sum -- [LeetCode](https://leetcode.com/problems/two-sum/) | [LeetCode CH](https://leetcode.cn/problems/two-sum/) (Easy) +- [LeetCode](https://leetcode.com/problems/two-sum/) | [LeetCode CH](https://leetcode.cn/problems/two-sum/) (Easy) - Return the indices of the two numbers such that they add up to a specific target. | Approach | Time Complexity | Space Complexity | -| -------- | --------------- | ---------------- | -| Hashmap | O(n) | O(n) | +| :------: | :-------------: | :--------------: | +| Hashmap | O(n) | O(n) | diff --git a/utils/integrate.py b/utils/integrate.py index 2ba3bd99..f84b6fac 100644 --- a/utils/integrate.py +++ b/utils/integrate.py @@ -4,66 +4,64 @@ import pandas as pd -from config import code, load_config_yaml +from config import code, load_config_yaml, sql_code class ProblemList: def __init__(self, file: str): self.src = "src" self.docs = "docs" - self.data = load_config_yaml(os.path.join(self.src, "config", file)) - self.dir = os.path.join(self.docs, self.data.dir) + self.df = pd.read_parquet(os.path.join("utils", "questions.parquet")) + self.cfg = load_config_yaml(os.path.join(self.src, "config", file)) + self.dir = os.path.join(self.docs, self.cfg.dir) os.makedirs(self.dir, exist_ok=True) - self.pattern = re.compile(r"^\d{4}_") - - # mkdocs - self.tab = 4 * " " - self.mkdocs_yaml = f"{self.tab}- {self.data.name}:\n" - self.mkdocs_yaml += ( - f"{self.tab}{self.tab}- Home: {self.data.dir}/index.md\n" - ) - # index - self.index_md_path = os.path.join(self.docs, self.data.dir, "index.md") + self.index_md_path = os.path.join(self.docs, self.cfg.dir, "index.md") if not os.path.exists(self.index_md_path): with open(self.index_md_path, "w") as outfile: - outfile.write(f"# {self.data.name}\n") + outfile.write(f"# {self.cfg.name}\n") - for topic, problems in self.data.topics.items(): - self.generate_topic_markdown(topic, problems) + self.pattern = re.compile(r"^\d{4}_") - print(self.mkdocs_yaml) + # mkdocs + self.mkdocs_yaml = f"- {self.cfg.name}:\n" + self.mkdocs_yaml += f" - Home: {self.cfg.dir}/index.md\n" - def generate_topic_markdown(self, topic: str, problems, comments=True): - files = [ - file - for problem in problems - for file in os.listdir(self.src) - if file.endswith(".py") - and self.pattern.match(file) - and int(file.split("_")[0]) == problem - ] + for topic, problems in self.cfg.topics.items(): + self.generate_topic_markdown(topic, problems) + def generate_topic_markdown(self, topic: str, problems, comments=True): title = f"# {topic}\n\n" md_name = topic.lower().replace(" ", "_") + ".md" - md_path = os.path.join(self.docs, self.data.dir, md_name) + md_path = os.path.join(self.docs, self.cfg.dir, md_name) - self.mkdocs_yaml += ( - f"{self.tab}{self.tab}- {topic}: {self.data.dir}/{md_name}\n" - ) + self.mkdocs_yaml += f" - {topic}: {self.cfg.dir}/{md_name}\n" with open(md_path, "w") as outfile: outfile.write(f"---\ncomments: {comments}\n---\n\n") outfile.write(title) - for file in files: - filename = file.split(".")[0] - md_file_path = os.path.join("docs", "md", filename + ".md") - with open(md_file_path, "r") as infile: - data = infile.read() - - outfile.write(data + code(filename) + "\n") + for qid in problems: + content = self.problem_markdown(qid) + outfile.write(content) + + def problem_markdown(self, qid: int): + row = self.df[self.df.QID == qid] + name = row["titleSlug"].values[0] + basename = str(qid).zfill(4) + "_" + name + filename = os.path.join(self.dir, basename + ".md") + + if os.path.exists(filename): + with open(filename, "r") as infile: + content = infile.read() + content += code(basename) + "\n" + else: + content = row["markdown"].values[0] + with open(filename, "w") as f: + f.write(content + "\n") + + return content if __name__ == "__main__":