### **Homework 0: Fibonacci Sequence with Error Handling** - **Deadline: 2025/10/15 23:59:59** ### **Goal of This Assignment (作業目標)** This is a warm-up assignment (Homework 0). The primary goal is **not** to test your programming skills, but to ensure that every student is familiar with the entire homework submission and grading workflow.\ 這是一份熱身作業(作業 0)。主要目的**不是**考驗你的程式能力,而是確保每位同學都熟悉整個作業繳交與評分的流程。 Through this assignment, you should:\ 透過這次作業,你應該要: - Understand how to access assignment requirements on the course website.\ 了解如何在課程網站上查看作業說明。 - Write a simple program that reads from standard input and writes to standard output.\ 撰寫一個能讀取標準輸入 (standard input) 並將結果輸出至標準輸出 (standard output) 的簡單程式。 - Implement basic error handling for invalid inputs.\ 能夠對無效的輸入進行基礎的錯誤處理。 - Successfully submit your code (main.py) to the **Gradescope** platform.\ 成功將你的程式碼 (main.py) 繳交至 **Gradescope** 平台。 - Be able to view the feedback and score from the autograder after submission.\ 能夠在繳交後,查看自動評分系統 (autograder) 所給予的回饋與分數。 ### **Problem Description (問題描述)** The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting with 0 and 1.\ 費氏數列是一個數列,其中每個數字是前兩個數字的和,由 0 和 1 開始。 The sequence is defined as:\ 其定義如下: - F(0) = 0 - F(1) = 1 - F(n) = F(n-1) + F(n-2), for n > 1 Your task is to write a Python program that takes a string from the input, determines if it's a valid non-negative integer, and then calculates the corresponding n-th Fibonacci number.\ 你的任務是寫一個 Python 程式來讀取一個輸入字串,判斷其是否為一個有效的非負整數,並計算出第 n 個費氏數。 ### **Input/Output Format (輸入/輸出格式)** - **Input:**\ The program will receive a single string from standard input. This string could be a valid representation of a non-negative integer (e.g., "10") or an invalid string (e.g., "djf383v", "-5").\ 你的程式將會從標準輸入讀取一個字串。這個字串可能是一個有效的非負整數(例如:"10"),也可能是一個無效的字串(例如:"djf383v" 或 "-5")。 - **Output:** - If the input is a valid non-negative integer n, the program should print the n-th Fibonacci number to standard output, followed by a newline character.\ 如果輸入是有效的非負整數 n,你的程式應該將第 n 個費氏數輸出到標準輸出,並以換行符號結尾。 - If the input is NOT a valid non-negative integer, the program should print 0.\ 如果輸入不是一個有效的非負整數,你的程式應該直接輸出 0。 - **Constraints (限制):** - If n is valid, it will be in the range 0 <= n <= 90. #### **Examples (範例):** - **Example 1 (Valid Input):** - Input: 8 - Output: 21 - **Example 2 (Invalid Input):** - Input: djf383v - Output: 0 * * * * * #### **1\. 關於輸出:** 自動評分系統 (Autograder) 只會檢查你的程式**「印到螢幕上」**的內容。 - **請務必使用** 如果你的函式是使用 return 回傳結果,請記得在主程式中接收這個結果並 print() 出來。 - **請確保只輸出數字答案本身。** 不要加上任何額外的提示文字,例如 "The answer is: " 或是 "input: ",任何多餘的文字都會導致評分失敗。 ``` # 正確示範 def my_fib_function(n): # ... calculates result ... return result # 主程式區塊 n = int(input()) final_answer = my_fib_function(n) print(final_answer) # <-- 關鍵!必須 print 出來 # 錯誤示範 # print("The result is:", final_answer) # <-- 這樣會因為多了提示文字而失敗 ``` #### **2\. 關於演算法效率:遞迴的陷阱 (About Algorithm Efficiency: The Recursion Trap)** 許多同學會直覺地使用**遞迴 (Recursion)** 來解決這個問題,這是一種很常見且符合邏輯的思路。 - def fib(n): return fib(n-1) + fib(n-2) 然而,這種「純粹」的遞迴解法有嚴重的**效能問題**。當 n 比較大時(例如 40 或 50),會產生海量的重複計算,導致程式執行時間遠遠超過系統限制(Timeout)。 - **溫馨提醒**:我們**強烈建議你使用「迭代法」(Iterative Approach**),也就是透過 for 或 while 迴圈來解決。從 F(0) 和 F(1) 開始,一步步推算出後面的數字,這樣效率會高非常多,才能在時間內通過所有測試案例。這是在演算法中非常重要的觀念。 * * * * * ### **Submission Guidelines (繳交方式)** - Please submit a single file named main.py to the "HW0" assignment on Gradescope.\ 請繳交一個名為 main.py 的檔案到 Gradescope 的 "HW0" 作業區。 - Your code should be written in Python 3.\ 你的程式碼需以 Python 3 撰寫。 ### **Grading (評分標準)** - Your submission will be graded automatically by the Gradescope autograder.\ 你的作業將由 Gradescope 的自動評分系統進行評分。 - There are **10 hidden test cases** in total, each worth **10 points**. Some test cases will specifically check for correct error handling and performance on large numbers.\ 總共有 **10 組隱藏的測試資料**,每一組佔 **10 分**。其中部分測資將專門測試你的錯誤處理,以及**程式在大數字下的執行效率**。 - Your final score for this assignment will be the total points from the test cases you pass.\ 你這份作業的最終分數,將是你通過的測試案例的總分。