Command Calculator

Command Calculator

Medium Python Control Flow 10 views
Explanation Complexity

Problem Statement

You will receive q queries of form: op a b, where op is ADD, SUB, MUL, DIV. For DIV, if b is 0 output ERR else output integer division a//b. Output answer for each query on new line.

Input Format

• First line: Integer q (number of queries)

• Next q lines: Each line contains op a b

• op is one of: ADD, SUB, MUL, DIV

• a and b are integers

Output Format

• For each query, print the result on a new line

• For DIV, if b == 0, print ERR

Example

4
ADD 5 3
SUB 10 4
MUL 2 6
DIV 8 2
8
6
12
4

Constraints

• 1 ≤ q ≤ 10^5

• -10^9 ≤ a, b ≤ 10^9

• Integer division should use a // b

Concept Explanation

Queries:

1.ADD 5 3 → 5 + 3 = 8

2.SUB 10 4 → 10 - 4 = 6

3.MUL 2 6 → 2 × 6 = 12

4.DIV 8 2 → 8 // 2 = 4

If a query was DIV 5 0, output would be ERR.

Step-by-Step Explanation

1.Read integer q.

2.Loop q times:

• Read op, a, b.

• If op is ADD, print a + b.

• If op is SUB, print a - b.

• If op is MUL, print a * b.

• If op is DIV:

• If b == 0, print "ERR".

• Otherwise, print a // b.

Concept Explanation

Queries:

1.ADD 5 3 → 5 + 3 = 8

2.SUB 10 4 → 10 - 4 = 6

3.MUL 2 6 → 2 × 6 = 12

4.DIV 8 2 → 8 // 2 = 4

If a query was DIV 5 0, output would be ERR.

Step-by-Step Explanation

1.Read integer q.

2.Loop q times:

• Read op, a, b.

• If op is ADD, print a + b.

• If op is SUB, print a - b.

• If op is MUL, print a * b.

• If op is DIV:

• If b == 0, print "ERR".

• Otherwise, print a // b.

Input / Output Format

Input Format
• First line: Integer q (number of queries)

• Next q lines: Each line contains op a b

• op is one of: ADD, SUB, MUL, DIV

• a and b are integers
Output Format
• For each query, print the result on a new line

• For DIV, if b == 0, print ERR
Constraints
• 1 ≤ q ≤ 10^5

• -10^9 ≤ a, b ≤ 10^9

• Integer division should use a // b

Examples

Input:
4 ADD 5 3 SUB 10 4 MUL 2 6 DIV 8 2
Output:
8 6 12 4

Example Solution (Public)

Python
import sysnlines=sys.stdin.read().splitlines()nif not lines or not lines[0].strip():n  sys.exit(0)nq=int(lines[0].strip())nout=[]nfor i in range(1,1+q):n  parts=(lines[i] if i<len(lines) else '').split()n  if len(parts)<3: continuen  op=parts[0]n  a=int(parts[1]); b=int(parts[2])n  if op=='ADD':n    out.append(str(a+b))n  elif op=='SUB':n    out.append(str(a-b))n  elif op=='MUL':n    out.append(str(a*b))n  else:n    if b==0:n      out.append('ERR')n    else:n      out.append(str(a//b))nsys.stdout.write('
'.join(out))

Official Solution Code

import sysnlines=sys.stdin.read().splitlines()nif not lines or not lines[0].strip():n  sys.exit(0)nq=int(lines[0].strip())nout=[]nfor i in range(1,1+q):n  parts=(lines[i] if i<len(lines) else '').split()n  if len(parts)<3: continuen  op=parts[0]n  a=int(parts[1]); b=int(parts[2])n  if op=='ADD':n    out.append(str(a+b))n  elif op=='SUB':n    out.append(str(a-b))n  elif op=='MUL':n    out.append(str(a*b))n  else:n    if b==0:n      out.append('ERR')n    else:n      out.append(str(a//b))nsys.stdout.write('
'.join(out))
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.