Type Aware Reduce

Type Aware Reduce

Hard Python Data Types 14 views
Explanation Complexity

Problem Statement

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: Integer n

• Second line: n space-separated values

• Each value is either an integer

• Or a float (contains a dot .)

Output Format

• If all values are integers → print the PRODUCT as an integer

•Otherwise → print the SUM as float with exactly 2 decimal places

Example

Sample Input 1
3
2 3 4

Sample Input 2
3
2 3.5 4
Sample Output 1
24

Sample Output 2
9.50

Constraints

• 1 ≤ n ≤ 10^5

• -10^9 ≤ value ≤ 10^9

• Values are valid integers or floats

Concept Explanation

Explanation 1

All values are integers.

Product = 2 × 3 × 4 = 24

So output is 24.

Explanation 2

Since one value (3.5) is a float,
we compute SUM instead.

Sum = 2 + 3.5 + 4 = 9.5

Print with 2 decimals → 9.50

Step-by-Step Explanation

1.Read integer n.

2.Read list of n values as strings.

3.Check if all values do not contain a dot .:

• If true:

• Convert all to integers.

• Multiply them together.

• Print product.

4.Otherwise:

• Convert all values to float.

• Compute sum.

• Print sum formatted to exactly 2 decimal places.

Concept Explanation

Explanation 1

All values are integers.

Product = 2 × 3 × 4 = 24

So output is 24.

Explanation 2

Since one value (3.5) is a float,
we compute SUM instead.

Sum = 2 + 3.5 + 4 = 9.5

Print with 2 decimals → 9.50

Step-by-Step Explanation

1.Read integer n.

2.Read list of n values as strings.

3.Check if all values do not contain a dot .:

• If true:

• Convert all to integers.

• Multiply them together.

• Print product.

4.Otherwise:

• Convert all values to float.

• Compute sum.

• Print sum formatted to exactly 2 decimal places.

Input / Output Format

Input Format
• First line: Integer n

• Second line: n space-separated values

• Each value is either an integer

• Or a float (contains a dot .)
Output Format
• If all values are integers → print the PRODUCT as an integer

•Otherwise → print the SUM as float with exactly 2 decimal places
Constraints
• 1 ≤ n ≤ 10^5

• -10^9 ≤ value ≤ 10^9

• Values are valid integers or floats

Examples

Input:
Sample Input 1 3 2 3 4 Sample Input 2 3 2 3.5 4
Output:
Sample Output 1 24 Sample Output 2 9.50

Example Solution (Public)

Python
import sysnp=sys.stdin.read().strip().split()nif not p: sys.exit(0)nn=int(p[0])na=p[1:1+n]nall_int=Truenfor s in a:n  if '.' in s:n    all_int=Falsen    breaknif all_int:n  prod=1n  for s in a:n    prod*=int(s)n  sys.stdout.write(str(prod))nelse:n  sm=0.0n  for s in a:n    sm+=float(s)n  sys.stdout.write(f'{sm:.2f}')

Official Solution Code

import sysnp=sys.stdin.read().strip().split()nif not p: sys.exit(0)nn=int(p[0])na=p[1:1+n]nall_int=Truenfor s in a:n  if '.' in s:n    all_int=Falsen    breaknif all_int:n  prod=1n  for s in a:n    prod*=int(s)n  sys.stdout.write(str(prod))nelse:n  sm=0.0n  for s in a:n    sm+=float(s)n  sys.stdout.write(f'{sm:.2f}')
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.