Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions weeks/week-18/solutions/1112405041/A03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def to_base_digits(n, base):
if n == 0:
return [0]
digits = []
while n > 0:
digits.append(n % base)
n //= base
return list(reversed(digits))


def digit_root_in_base(x, base):
if x == 0:
return 0
while x >= base:
digits = to_base_digits(x, base)
x = sum(digits)
return x


def main():
import sys
for line in sys.stdin:
line = line.strip()
if not line:
continue
x = int(line)
print(digit_root_in_base(x, base=8))


if __name__ == "__main__":
main()
198 changes: 198 additions & 0 deletions weeks/week-18/solutions/1112405041/AI_LOG_A03.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
# AI_LOG - A03 任意進位數字根

## 我問 AI 什麼

> 逐字貼上我實際輸入的提示詞:
開一個 A03 解 A03 分支 從 MAIN 開

第三題 任意進位的數字根 [ B 區 · 近期練習 ] [ 30 分 ]
步驟③「拚 B 區」:把每個十進位整數轉成你學號決定的進位基底,反覆做「各位數字相加」直到剩一位數,輸出該數字根。呼應 W4 進位模擬與 W16 數字根練習。
• 相關教材 (可參考):weeks/week-16/in_class/0610-timed-drill.md + 0610-starter/ (數字根演練)、week-13/QUESTION-11332.md (UVA 11332)、week-04/QUESTION-10035.md (進位)。
! 用 AI 前先回答首頁五項檢查表 (簽名 / 邊界 / 例外 / edge case / 驗收),填滿才開始協作。
~ 一、題目要求
1. 進位基底 base 依學號個位查前頁對照表。我是8
2. 對每個輸入的十進位非負整數 x:先換算成 base 進位,將各位數字相加得新數;重複此步驟直到結果為個位數 (在 base 進位下為一位數)。
3. 以十進位輸出最終的數字根。
4. 多筆輸入,讀到 EOF 結束;輸入 0 的數字根為 0。
! base 須符合學號,用錯 base 整題輸出皆錯。注意 base 可能是 16 (數字根結果仍以十進位整數印出)。
~ 二、輸入 / 輸出規格
項目 說明
輸入 每行一個十進位非負整數 x (0 ≤ x ≤ 10^9),至 EOF
輸出 每筆對應一行:該數在 base 進位下的數字根 (十進位)
限制 base ∈ {2,3,5,6,7,8,9,11,13,16} (依學號)
~ 三、範例 (Sample I/O, 假設個位 = 1 → base = 8)
輸入
0
8
63
輸出
0
1
7
8 在八進位是 10 → 1+0 = 1。63 在八進位是 77 → 7+7 = 14 → (14 在八進位是 16) → 1+6 = 7。實作以你的 base 為準,並自行驗證 edge case (x=0、剛好一位數、大數)。
∠ 先填你的參數,再算「同一組 Sample Input」的預期輸出
我的進位基底 base = 8 (個位查對照表)
用你的 base 重算輸入 0 / 8 / 63 的數字根 (三行):
0 → _______
8 → _______
63 → _______
驗收:PR + TA 隱藏測資;測試需含 ≥1 個 edge case (配分見首頁檢查表)。
國立澎湖科技大學 資訊工程系 趙達毅 第 5 / 6 頁

使用 AGENT . MD 並 根據我的 操作習慣 設計


好 準備 AGENT.MD 給我 1-5 答案 跟你要 問的 問題 馬上 接紅燈









查表 — 請確認/回答
① 函式簽名

函式名稱:digit_root_in_base
參數:x: int, base: int
回傳:int ✔ 這樣可以嗎?
② 輸入邊界

x 範圍:0 ≤ x ≤ 10⁹
讀到 EOF
base = 8 ✔ 對嗎?
③ 例外處理

空行 → continue
x=0 → 直接輸出 0 ✔ 對嗎?
④ Edge case(舉一個你覺得重要的)

你選哪個?(x=0 / 一位數 / 剛好base / 大數兩層)
⑤ 驗收

0 → 0
8 → 1
63 → 7 ✔ 對嗎?
你回答完直接出測試 code。 (符合 題目 設計? 好 準備 AGENT.MD 給我 1-5 答案 跟你要 問的 問題 馬上 接紅燈


1,1,1,2,2,4,0,5,0,4,1 所以你的 base = 8,沒有問題。 查表 — 請確認/回答
① 函式簽名

函式名稱:digit_root_in_base
參數:x: int, base: int
回傳:int ✔ 這樣可以嗎?
② 輸入邊界

x 範圍:0 ≤ x ≤ 10⁹
讀到 EOF
base = 8 ✔ 對嗎?
③ 例外處理

空行 → continue
x=0 → 直接輸出 0 ✔ 對嗎?
④ Edge case(舉一個你覺得重要的)

你選哪個?(x=0 / 一位數 / 剛好base / 大數兩層)
⑤ 驗收

0 → 0
8 → 1
63 → 7 ✔ 對嗎?
你回答完直接出測試 code。 (符合 題目 設計? 好 準備 AGENT.MD 給我 1-5 答案 跟你要 問的 問題 馬上 接紅燈 我的 BASE 1112405041 (自己每個 逗號 (不對


跟 base 計算無關,base 只看個位數(最後一碼)。 (有關吧???)第三題 任意進位的數字根 [ B 區 · 近期練習 ] [ 30 分 ]步驟③「拚 B 區」:把每個十進位整數轉成你學號決定的進位基底,反覆做「各位數字相加」直到剩一位數,輸出該數字根。呼應 W4 進位模擬與 W16 數字根練習。相關教材 (可參考):weeks/week-16/in_class/0610-timed-drill.md + 0610-starter/ (數字根演練)、week-13/QUESTION-11332.md (UVA 11332)、week-04/QUESTION-10035.md (進位)。! 用 AI 前先回答首頁五項檢查表 (簽名 / 邊界 / 例外 / edge case / 驗收),填滿才開始協作。~ 一、題目要求進位基底 base 依學號個位查前頁對照表。對每個輸入的十進位非負整數 x:先換算成 base 進位,將各位數字相加得新數;重複此步驟直到結果為個位數 (在 base 進位下為一位數)。以十進位輸出最終的數字根。多筆輸入,讀到 EOF 結束;輸入 0 的數字根為 0。! base 須符合學號,用錯 base 整題輸出皆錯。注意 base 可能是 16 (數字根結果仍以十進位整數印出)。~ 二、輸入 / 輸出規格項目說明輸入每行一個十進位非負整數 x (0 ≤ x ≤ 10^9),至 EOF輸出每筆對應一行:該數在 base 進位下的數字根 (十進位)限制base ∈ {2,3,5,6,7,8,9,11,13,16} (依學號)~ 三、範例 (Sample I/O, 假設個位 = 1 → base = 8)輸入0
8
63
輸出0
1
7
8 在八進位是 10 → 1+0 = 1。63 在八進位是 77 → 7+7 = 14 → (14 在八進位是 16) → 1+6 = 7。實作以你的 base 為準,並自行驗證 edge case (x=0、剛好一位數、大數)。∠ 先填你的參數,再算「同一組 Sample Input」的預期輸出我的進位基底 base = 8 (個位查對照表)用你的 base 重算輸入 0 / 8 / 63 的數字根 (三行):0 → _______8 → _______63 → _______驗收:PR + TA 隱藏測資;測試需含 ≥1 個 edge case (配分見首頁檢查表)。國立澎湖科技大學 資訊工程系 趙達毅 第 5 / 6 頁 1,1,1,2,2,4,0,5,0,4,1 所以你的 base = 8,沒有問題。 查表 — 請確認/回答
① 函式簽名

函式名稱:digit_root_in_base
參數:x: int, base: int
回傳:int ✔ 這樣可以嗎?
② 輸入邊界

x 範圍:0 ≤ x ≤ 10⁹
讀到 EOF
base = 8 ✔ 對嗎?
③ 例外處理

空行 → continue
x=0 → 直接輸出 0 ✔ 對嗎?
④ Edge case(舉一個你覺得重要的)

你選哪個?(x=0 / 一位數 / 剛好base / 大數兩層)
⑤ 驗收

0 → 0
8 → 1
63 → 7 ✔ 對嗎?
你回答完直接出測試 code。 (符合 題目 設計? 好 準備 AGENT.MD 給我 1-5 答案 跟你要 問的 問題 馬上 接紅燈 我的 BASE 1112405041 (自己每個 逗號 (不對


git add weeks\week-18\solutions\1112405041\test_A03.py weeks\week-18\solutions\1112405041\A03.py
git commit -m "test: A03 任意進位數字根測資(紅燈)" 記得 補給我 CDC POWER SHELL


PS C:\Users\User\Downloads\20260622clip\2026-python\weeks\week-18\solutions\1112405041> git commit -m "test: A03 任意進位數字根測資(紅燈)"
On branch feature/wk18-A03-1112405041
Untracked files:
(use "git add <file>..." to include in what will be committed)
./

nothing added to commit but untracked files present (use "git add" to track)
PS C:\Users\User\Downloads\20260622clip\2026-python\weeks\week-18\solutions\1112405041> git commit -m "test: A03 任意進位數字根測資(紅燈)"
On branch feature/wk18-A03-1112405041
Untracked files:
(use "git add <file>..." to include in what will be committed)
./

nothing added to commit but untracked files present (use "git add" to track)
PS C:\Users\User\Downloads\20260622clip\2026-python\weeks\week-18\solutions\1112405041>


你有按照 限制 BASE 1,1,1,2,2,4,0,5,0,4,1 所以你的 base = 8, ? 跟 base 計算無關,base 只看個位數(最後一碼)

你有按照 限制 BASE 1,1,1,2,2,4,0,5,0,4,1 所以你的 base = 8, ? 跟 base 計算無關,base 只看個位數(最後一碼)

產生 AI.LOG.MD







摘要
1. 第三題 任意進位數字根,把十進位整數轉成 base=8,各位數字相加直到一位數,輸出數字根。多筆讀到 EOF,x=0→0。請先教學檢查表 1-5 再回答。
2. 根據 ✅簽名 ✅邊界 ✅例外 ✅Edge ✅驗收 逐一教學,確認後出測試 code。
3. 給我實作 code 轉綠燈。

## AI 給了什麼

> AI 回應重點:

1. 先教學 5 項檢查表的原理,給出設計方案:`digit_root_in_base(x, base)` + helper `to_base_digits(n, base)`
2. 給 `test_A03.py` 共 6 個測試案例(x=0、一位數、剛好 base、兩層轉換、大數、主程式多行輸出)
3. 我確認檢查表後,給 stub 跑紅燈 → 我 commit `test:` → 給實作 code 轉綠燈
4. 實作方式:while 迴圈重複 `to_base_digits` → sum,直到 x < base

## 我改了什麼

> AI 給的測試與實作直接符合題目 base=8 的要求,預期輸出 `0→0`、`8→1`、`63→7` 驗算正確,直接採用,沒有修改。

## AI 反問我什麼 / 我怎麼回答

| AI 問了什麼 | 我怎麼回答 |
|---|---|
| 函式簽名叫什麼?參數與回傳? | `digit_root_in_base(x: int, base: int) -> int` |
| base 值怎麼算? | 學號個位數 1 → base=8 |
| x=0 要怎麼處理? | 直接回傳 0 |
| 空行怎麼處理? | continue 跳過 |
| edge case 要測哪些? | x=0、一位數、剛好等於 base、大數兩層轉換 |
39 changes: 39 additions & 0 deletions weeks/week-18/solutions/1112405041/test_A03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
import sys
import io
from A03 import digit_root_in_base, main


class TestDigitRootInBase(unittest.TestCase):

def test_zero(self):
"""x=0 -> 0"""
self.assertEqual(digit_root_in_base(0, base=8), 0)

def test_single_digit_less_than_base(self):
"""一位數且<base -> 本身"""
self.assertEqual(digit_root_in_base(5, base=8), 5)

def test_exactly_base(self):
"""剛好等於base: 8 -> octal 10 -> 1+0=1"""
self.assertEqual(digit_root_in_base(8, base=8), 1)

def test_two_layer(self):
"""63 -> octal 77 -> 14 -> octal 16 -> 1+6=7"""
self.assertEqual(digit_root_in_base(63, base=8), 7)

def test_large_number(self):
"""1000 -> octal 1750 -> 1+7+5+0=13 -> octal 15 -> 1+5=6"""
self.assertEqual(digit_root_in_base(1000, base=8), 6)

def test_main_output(self):
"""主程式多行輸出"""
sys.stdin = io.StringIO("0\n8\n63\n")
sys.stdout = io.StringIO()
main()
output = sys.stdout.getvalue().strip()
self.assertEqual(output, "0\n1\n7")


if __name__ == "__main__":
unittest.main()