Type Aware Reduce
Python
Hard
3 views
Problem Description
Read n values (each is int or float with dot). If all are integers, output PRODUCT as integer. Otherwise output SUM as float with 2 decimals.
Input Format
First line n. Second line n values.
Output Format
One number output.
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]
all_int=True
for s in a:
if '.' in s:
all_int=False
break
if all_int:
prod=1
for s in a:
prod*=int(s)
sys.stdout.write(str(prod))
else:
sm=0.0
for s in a:
sm+=float(s)
sys.stdout.write(f'{sm:.2f}')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!