Back to Practice Dashboard
Python BasicsEasy

Octal to Binary conversion

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

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?
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