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 资源

通过相关的交互式教程、备忘单和代码比较来扩展您的知识。