Binary, Hex and Octal Literal Reader

Medium ⏱ 12 min 60% acceptance ★★★★★ 4.9
Write a function parse_number(text) that accepts a string representing a number in binary ("0b..."), octal ("0o..."), hexadecimal ("0x..."), or plain decimal, and returns its integer value. Use int() with base 0 so the prefix determines the base automatically.

Examples

Example 1
Input
parse_number("0b101")
Output
5
Explanation

Binary 101 = 5 in decimal.

Example 2
Input
parse_number("0x1A")
Output
26
Explanation

Hex 1A = 26 in decimal.

Example 3
Input
parse_number("42")
Output
42
Explanation

Plain decimal string.

Constraints

  • Input is always a valid literal Python could parse this way.

Topics

Variables & Data Typesnumber bases

Companies

CiscoIntel

Hints

Hint 1

`int(text, 0)` lets Python infer the base from the prefix.

Hint 2

Base 0 falls back to base-10 when there is no recognized prefix.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.