Loading...
    • 開發者指南
    • API 參考
    • MCP
    • 資源
    • 發行說明
    Search...
    ⌘K
    資源
    概覽術語表系統提示詞
    提示詞庫宇宙按鍵企業預言家網站精靈Excel 公式專家Google Apps 腳本編寫器Python 除錯大師時光旅行顧問故事創作夥伴引用來源SQL 魔法師解夢師雙關語大師美食創作家混成詞詩人幽默助手 HalLaTeX 達人情緒色彩師Git 高手比喻達人倫理困境導航會議記錄員慣用語解析器程式碼顧問函式製造器新詞創造者CSV 轉換器Emoji 編碼器文章潤色師多角度思考者冷知識產生器正念導師二年級簡化器VR 健身創新者PII 淨化器備忘錄大師職涯教練評分大師繞口令面試問題設計師文法精靈猜謎語程式碼解說員外星人類學家資料整理器品牌建構師效率評估器評論分類器方向解碼器勵志繆思電子郵件擷取器首席審核員課程規劃師蘇格拉底智者頭韻煉金師未來時尚顧問多語言超能力產品命名專家哲學沉思試算表魔法師科幻情境模擬器自適應編輯器巴別塔廣播推文語氣偵測器機場代碼分析師
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    提示詞庫

    函式製造器

    根據詳細規格建立 Python 函式。

    將此提示複製到我們的開發者 Console 中親自試用!

    內容
    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 請求

    Was this page helpful?

    • API 請求