Python 基礎知識簡單

八進位到十進制轉換

“八進位到十進制轉換”問題的詳細指南和 Python 實作。

問題陳述

簡單

寫一個函數 octal_to_decimal(octal_str) ,它接受一個表示八進制數(以 8 為基數,數字 0-7)的字串,並傳回其等價的十進制(以 10 為基數)整數。

八進制數中的每個數字代表 8 的冪,從最右邊的數字 (8^0) 開始。

約束條件
  • 1 <= len(octal_str) <= 10
  • octal_str contains only digits '0' through '7'

範例

Example 1
Input
octal_to_decimal('17')
Output
15
Explanation

1×8¹ + 7×8⁰ = 8 + 7 = 15.

Example 2
Input
octal_to_decimal('144')
Output
100
Explanation

1×8² + 4×8¹ + 4×8⁰ = 64 + 32 + 4 = 100.

Example 3
Input
octal_to_decimal('10')
Output
8
Explanation

1×8¹ + 0×8⁰ = 8.

Need a Hint?
考慮使用特定於數字的資料結構,例如集合或堆。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。