写一个刷题代码用python
时间: 2023-12-17 09:19:59 浏览: 32
好的,以下是一个简单的用Python语言编写的刷题代码:
```python
# 导入需要的模块
import requests
# 定义一个函数,用来获取LeetCode的题目列表
def get_problem_list():
# LeetCode的API地址
url = "https://leetcode.com/api/problems/all/"
# 发送请求
response = requests.get(url)
# 将题目列表转换为json格式
problem_list = response.json()
# 遍历题目列表,输出题目的编号和名称
for problem in problem_list["stat_status_pairs"]:
print(problem["stat"]["question_id"], problem["stat"]["question__title"])
# 定义一个函数,用来获取指定编号的题目的详细信息
def get_problem_detail(problem_id):
# LeetCode的API地址
url = f"https://leetcode.com/api/problems/{problem_id}/"
# 发送请求
response = requests.get(url)
# 将题目详细信息转换为json格式
problem_detail = response.json()
# 输出题目的标题和描述
print(problem_detail["data"]["question"]["frontend_question_id"], problem_detail["data"]["question"]["title"])
print(problem_detail["data"]["question"]["content"])
# 测试函数
if __name__ == '__main__':
# 获取题目列表
get_problem_list()
# 获取指定编号的题目的详细信息
get_problem_detail(1)
```
以上是一个简单的用Python语言编写的刷题代码,可以获取LeetCode的题目列表和指定编号的题目的详细信息。具体使用方法可以根据实际需求进行调整。