このプロンプトを開発者向けコンソールにコピーして、自分で試してみましょう!

内容
Systemあなたの任務は、提供された自然言語リクエストに基づいてPython関数を作成することです。リクエストには、入力パラメータと期待される戻り値を含む、関数の望ましい機能が記述されています。与えられた仕様に従って関数を実装し、エッジケースを処理し、必要な検証を実行し、Pythonプログラミングのベストプラクティスに従うようにしてください。コードにはロジックを説明し、他の開発者が実装を理解するのに役立つ適切なコメントを含めてください。
User数独パズルを解く関数が欲しいです。この関数は9x9の数独グリッドを入力として受け取り、空のセルは値0で表されます。関数はバックトラッキングアルゴリズムを使用してパズルを解き、解かれたグリッドを返す必要があります。パズルが解けない場合は、Noneを返す必要があります。また、入力グリッドが有効な数独パズルであることを確認するための検証も行う必要があります。

出力例

Python
def solve_sudoku(grid):
   def is_valid(row, col, num):
       # 現在の行と列で数字が有効かどうかをチェック
       for i in range(9):
           if grid[row][i] == num or grid[i][col] == num:
               return False

       # 現在の3x3サブグリッドで数字が有効かどうかをチェック
       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 row == 9:
           return True

       # 現在の行の終わりに達した場合、次の行に移動
       if col == 9:
           return backtrack(row + 1, 0)

       # 現在のセルがすでに埋まっている場合、次のセルに移動
       if grid[row][col] != 0:
           return backtrack(row, col + 1)

       # 現在のセルに1から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

       # 有効な数字が見つからない場合、バックトラック
       return False

   # 入力グリッドを検証
   if not all(len(row) == 9 and all(0 <= cell <= 9 for cell in row) for row in grid):
       return None

   # バックトラッキングアルゴリズムを開始
   if backtrack(0, 0):
       return grid
   else:
       return None


APIリクエスト

import anthropic

client = anthropic.Anthropic(  # デフォルトはos.environ.get("ANTHROPIC_API_KEY")
    api_key="my_api_key",
)
message = client.messages.create(
    model="claude-opus-4-20250514",
    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)