Python Basics簡単

Binary to Decimal conversion

Detailed guide and Python implementation for the 'Binary to Decimal conversion' problem.

問題提起

簡単

Write a function binary_to_decimal(binary_str) that takes a string representing a binary number (containing only '0' and '1') and returns its decimal (base-10) integer equivalent.

Each digit in a binary number represents a power of 2, starting from the rightmost digit (2^0). For example, binary '1010' = 1×2³ + 0×2² + 1×2¹ + 0×2⁰ = 8 + 0 + 2 + 0 = 10.

制約
  • 1 <= len(binary_str) <= 20
  • binary_str contains only '0' and '1'

Example 1
Input
binary_to_decimal('1010')
Output
10
Explanation

1×2³ + 0×2² + 1×2¹ + 0×2⁰ = 8 + 0 + 2 + 0 = 10.

Example 2
Input
binary_to_decimal('1111')
Output
15
Explanation

1×2³ + 1×2² + 1×2¹ + 1×2⁰ = 8 + 4 + 2 + 1 = 15.

Example 3
Input
binary_to_decimal('10000')
Output
16
Explanation

1×2⁴ = 16.

Need a Hint?
Consider using Numbers-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

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 リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。