Sum Numbers With Prefix Base
Python
Hard
3 views
Problem Description
Read n tokens. Each token can be decimal like 15, binary like 0b1010, octal like 0o12, or hex like 0xFF. Tokens can also have a leading '-' sign. Convert all to integer and output the sum.
Input Format
First line n. Second line n tokens.
Output Format
One integer sum.
Sample Test Case
Input:
4
0b1010 -0x1 12 0o7
Official Solution
import sys
p=sys.stdin.read().strip().split()
if not p: sys.exit(0)
n=int(p[0])
a=p[1:1+n]
sm=0
for t in a:
sm+=int(t,0)
sys.stdout.write(str(sm))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!