Back to Practice Dashboard
Python BasicsEasy

Binary to Decimal conversion

Learn how to solve the 'Binary to Decimal conversion' problem. This detailed resource details brute force and optimized approaches.

Problem Statement

Easy

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.

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

Examples

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?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

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

Open in Editor