Prompt 素材库
- 提示库
- 宇宙按键
- 企业预测分析
- 网站向导
- Excel公式专家
- Google Apps 脚本编写器
- Python 代码调试器
- 时间旅行顾问
- 故事创作助手
- 引用你的来源
- SQL 魔法师
- 解梦专家
- 双关语大师
- 美食创作者
- 混合词诗人
- 幽默助手哈尔
- LaTeX 指南
- 情绪色彩转换器
- Git 精通
- 比喻大师
- 伦理困境导航
- 会议记录员
- 成语解析器
- 代码顾问
- 函数生成器
- 新词创造者
- CSV转换器
- 表情符号编码器
- 文章润色器
- 观点权衡者
- 知识问答生成器
- 正念导师
- 二年级简化器
- VR健身创新者
- PII 信息净化器
- 备忘录大师
- 职业教练
- 评分专家
- 绕口令
- 面试问题生成器
- 语法精灵
- 猜谜语
- 代码解释器
- 外星人类学家
- 数据整理器
- 品牌建设者
- 效率估算器
- 评论分类器
- 方向解码器
- 激励缪斯
- 电子邮件提取器
- 主内容审核员
- 课程计划制定者
- 苏格拉底式智者
- 头韵炼金术士
- 未来主义时尚顾问
- 多语言超能力
- 产品命名专家
- 哲学思考
- 电子表格魔法师
- 科幻场景模拟器
- 自适应编辑器
- 巴别塔的广播
- 推文语气检测器
- 机场代码分析器
Prompt 素材库
函数生成器
根据详细规范创建 Python 函数。
将此提示复制到我们的开发者控制台中亲自尝试!
内容 | |
---|---|
System | 你的任务是根据提供的自然语言请求创建 Python 函数。这些请求将描述函数所需的功能,包括输入参数和预期的返回值。根据给定的规范实现函数,确保处理边缘情况,执行必要的验证,并遵循 Python 编程的最佳实践。请在代码中包含适当的注释来解释逻辑并帮助其他开发人员理解实现。 |
User | 我想要一个可以解决数独谜题的函数。该函数应该接受一个 9x9 的数独网格作为输入,其中空单元格用数值 0 表示。该函数应该使用回溯算法解决谜题并返回已解决的网格。如果谜题无法解决,它应该返回 None。该函数还应该验证输入网格以确保它是一个有效的数独谜题。 |
示例输出
Python
def solve_sudoku(grid):
def is_valid(row, col, num):
# Check if the number is valid in the current row and column
for i in range(9):
if grid[row][i] == num or grid[i][col] == num:
return False
# Check if the number is valid in the current 3x3 subgrid
start_row, start_col = (row // 3) * 3, (col // 3) * 3
for i in range(3):
for j in range(3):
if grid[start_row + i][start_col + j] == num:
return False
return True
def backtrack(row, col):
# If we have filled all cells, the puzzle is solved
if row == 9:
return True
# Move to the next row if we have reached the end of the current row
if col == 9:
return backtrack(row + 1, 0)
# If the current cell is already filled, move to the next cell
if grid[row][col] != 0:
return backtrack(row, col + 1)
# Try filling the current cell with numbers from 1 to 9
for num in range(1, 10):
if is_valid(row, col, num):
grid[row][col] = num
if backtrack(row, col + 1):
return True
grid[row][col] = 0
# If no valid number is found, backtrack
return False
# Validate the input grid
if not all(len(row) == 9 and all(0 <= cell <= 9 for cell in row) for row in grid):
return None
# Start the backtracking algorithm
if backtrack(0, 0):
return grid
else:
return None
API 请求
import anthropic
client = anthropic.Anthropic( # defaults to os.environ.get("ANTHROPIC_API_KEY")
api_key="my_api_key",
)
message = client.messages.create(
model="claude-3-7-sonnet-20250219",
max_tokens=1000,
temperature=0,
system="Your task is to create Python functions based on the provided natural language requests. The requests will describe the desired functionality of the function, including the input parameters and expected return value. Implement the functions according to the given specifications, ensuring that they handle edge cases, perform necessary validations, and follow best practices for Python programming. Please include appropriate comments in the code to explain the logic and assist other developers in understanding the implementation.",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "I want a function that can solve a Sudoku puzzle. The function should take a 9x9 Sudoku grid as input, where empty cells are represented by the value 0. The function should solve the puzzle using a backtracking algorithm and return the solved grid. If the puzzle is unsolvable, it should return None. The function should also validate the input grid to ensure it is a valid Sudoku puzzle.",
}
],
}
],
)
print(message.content)