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
    提示詞庫

    程式碼解說器

    用淺顯易懂的語言簡化並解釋複雜的程式碼。

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

    內容
    SystemYour task is to take the code snippet provided and explain it in simple, easy-to-understand language. Break down the code's functionality, purpose, and key components. Use analogies, examples, and plain terms to make the explanation accessible to someone with minimal coding knowledge. Avoid using technical jargon unless absolutely necessary, and provide clear explanations for any jargon used. The goal is to help the reader understand what the code does and how it works at a high level.
    Userimport random def bubble*sort(arr): n = len(arr) for i in range(n-1): for j in range(n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] return arr numbers = [random.randint(1, 100) for * in range(10)] print("Unsorted array:", numbers) sorted_numbers = bubble_sort(numbers) print("Sorted array:", sorted_numbers)

    範例輸出

    這段程式碼展示了氣泡排序演算法的實作,用於將數字列表按升序排列。讓我們逐步分解:

    1. 程式碼首先匯入 'random' 模組,該模組將用於為範例生成隨機數字。

    2. 定義了 'bubble_sort' 函式,它接受一個數字陣列(列表)作為輸入。這個函式將執行排序操作。

    3. 在 'bubble_sort' 函式內部,變數 'n' 被賦值為輸入陣列的長度。這將用於確定排序所需的迭代次數。

    4. 第一個迴圈由變數 'i' 控制,迭代 'n-1' 次。這是因為經過 'n-1' 次遍歷後,最大的數字將位於陣列末端,所以我們不需要再次比較它。

    5. 第二個迴圈由變數 'j' 控制,迭代 'n-i-1' 次。這是因為每次遍歷後,未排序元素中最大的數字將被放置在末端,所以我們在下一次遍歷中不需要比較它。

    6. 在第二個迴圈內部,有一個 if 語句比較陣列中相鄰的元素。如果索引 'j' 處的元素大於索引 'j+1' 處的元素,它們就會被交換。這確保了較大的元素向陣列末端移動。

    7. 交換是使用 Python 語法 'arr[j], arr[j+1] = arr[j+1], arr[j]' 完成的,它同時將 'arr[j+1]' 的值賦給 'arr[j]',並將 'arr[j]' 的值賦給 'arr[j+1]'。

    8. 所有迭代完成後,'bubble_sort' 函式返回排序後的陣列。

    9. 在範例程式碼中,使用列表推導式生成了一個包含 10 個介於 1 到 100 之間的隨機數字的列表:'[random.randint(1, 100) for _ in range(10)]'。底線 '_' 用作佔位變數,因為我們不需要索引值。

    10. 列印未排序的陣列以顯示數字的初始順序。

    11. 以 'numbers' 陣列作為輸入呼叫 'bubble_sort' 函式,排序後的陣列儲存在 'sorted_numbers' 變數中。

    12. 最後,列印排序後的陣列以顯示按升序排列的數字。

    用更簡單的話來說,想像你有一個順序混亂的數字列表。氣泡排序演算法的工作方式是將每個數字與旁邊的數字進行比較,如果它們的順序不對,就交換它們。這個過程重複多次,直到整個列表排序完成。就像氣泡上升到水面一樣,較大的數字在每次遍歷中逐漸向列表末端移動。


    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-opus-4-6",
        max_tokens=1000,
        temperature=0,
        system="Your task is to take the code snippet provided and explain it in simple, easy-to-understand language. Break down the code's functionality, purpose, and key components. Use analogies, examples, and plain terms to make the explanation accessible to someone with minimal coding knowledge. Avoid using technical jargon unless absolutely necessary, and provide clear explanations for any jargon used. The goal is to help the reader understand what the code does and how it works at a high level.",
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "import random\n\ndef bubble_sort(arr):\n    n = len(arr)\n    for i in range(n-1):\n        for j in range(n-i-1):\n            if arr[j] > arr[j+1]:\n                arr[j], arr[j+1] = arr[j+1], arr[j]\n    return arr\n\nnumbers = [random.randint(1, 100) for _ in range(10)]\nprint(\"Unsorted array:\", numbers)\nsorted_numbers = bubble_sort(numbers)\nprint(\"Sorted array:\", sorted_numbers)"
                    }
                ]
            }
        ]
    )
    print(message.content)
    

    Was this page helpful?

    • API 請求