Base to Decimal Safely
Python
Medium
2 views
Problem Description
Read a string num and base b (2 to 36). Convert num to decimal and output it. If conversion fails, output INVALID.
Input Format
One line: num base.
Output Format
Decimal or INVALID.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if len(p)<2:
sys.stdout.write('INVALID')
else:
num=p[0]
try:
base=int(p[1])
if base<2 or base>36:
raise ValueError()
sys.stdout.write(str(int(num,base)))
except Exception:
sys.stdout.write('INVALID')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!