MCPcopy
hub / github.com/doocs/leetcode / generate_question_readme

Function generate_question_readme

solution/util.py:161–268  ·  view source on GitHub ↗
(result)

Source from the content-addressed store, hash-verified

159
160
161def generate_question_readme(result):
162 for item in result:
163 if not item["content_cn"] and not item["content_en"]:
164 print(f"Skip {item['frontend_question_id']} {item['title_cn']}")
165 continue
166 path = (
167 f'./{item["sub_folder"]}/{item["frontend_question_id"]}.{item["title_en"]}'
168 )
169 path = path.replace(":", " ")
170 if os.path.isdir(path):
171 continue
172 os.makedirs(path)
173
174 # choose the readme template
175 category = item["category"]
176 readme_template_cn, readme_template_en = select_templates(category)
177 paid_only = " 🔒" if item["paid_only"] else ""
178 rating_item = rating_dict.get(str(item["frontend_question_id"]))
179 rating = rating_item.get("Rating", 0) if rating_item else ""
180 source = (
181 rating_item.get("ContestID_zh") + " " + rating_item.get("ProblemIndex")
182 if rating_item
183 else ""
184 )
185 # 生成 metadata
186 """
187 ---
188 tags:
189 - 数组
190 - 哈希表
191 difficulty: 简单
192 rating: 1234
193 comments: true
194 edit_url: https://github.com/doocs/leetcode/edit/main/solution/0000-0099/0001.Two%20Sum/README.md
195 ---
196 """
197 cat = category_dict.get(category, category)
198 cat = cat.title() if cat and cat[0].islower() else cat
199 metadata = {
200 "tags": item["tags_cn"] or [cat],
201 "difficulty": item["difficulty_cn"],
202 "rating": rating,
203 "comments": True,
204 "edit_url": f'https://github.com/doocs/leetcode/edit/main{item["relative_path_cn"]}',
205 "source": source,
206 }
207 if not item["tags_cn"] or metadata["tags"] == ["Algorithms"]:
208 metadata.pop("tags")
209 if not rating:
210 metadata.pop("rating")
211 if not source:
212 metadata.pop("source")
213 yaml_metadata = yaml.dump(
214 metadata, default_flow_style=False, allow_unicode=True
215 )
216 metadata_section = f"---\n{yaml_metadata}---"
217
218 # generate lc-cn problem readme

Callers 1

runFunction · 0.85

Calls 5

printFunction · 0.85
select_templatesFunction · 0.85
formatMethod · 0.80
getMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected