Python BasicsEasy

Octal to Binary conversion

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

Problem Statement

Easy

Write a function octal_to_binary(octal_str) that takes a string representing an octal number (base-8, digits 0-7) and returns a string representing its binary (base-2) equivalent. Do not include leading zeros in the output (except for input '0').

Hint: Convert each octal digit to its 3-bit binary equivalent and concatenate.

Constraints
  • 1 <= len(octal_str) <= 10
  • octal_str contains only digits '0' through '7'

Examples

Example 1
Input
octal_to_binary('12')
Output
'1010'
Explanation

1 = 001, 2 = 010. Concatenate: 001010. Remove leading zeros: 1010.

Example 2
Input
octal_to_binary('77')
Output
'111111'
Explanation

7 = 111, 7 = 111. Concatenate: 111111.

Example 3
Input
octal_to_binary('312')
Output
'11001010'
Explanation

3 = 011, 1 = 001, 2 = 010. Concatenate: 011001010. Remove leading zero: 11001010.

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

Ready to Solve?

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

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.