1

so I am doing an exercise from SPOJ, and it's a simple calculator. Everytime I try to submit an answer I get an NZEC error, and I wonder if it's because it should be defined as int32. Here's my code:

import sys
n = input()
n = int(n)


i = 0
while n > i:
    znak, num1, num2 = input().split()
    num1 = int(num1)
    num2 = int(num2)
    if znak == "+":
        b = num1 + num2
        print(b)
    elif znak == "-":
        b = num1 - num2
        print(b)
    elif znak == "*":
        b = num1 * num2
        print(b)
    elif znak == "/":
        b = num1 / num2
        print(b)
    elif znak == "%":
        b = num1 % num2
        print(b)
    i += 1

sys.exit(0)

I've tried a some "solutions" for this NZEC error, but nothing worked.

9
  • There's no need to use sys.exit, the program exits automatically. Commented Mar 24, 2017 at 10:19
  • Just wanted to make sure if it works properly for their compilator. Commented Mar 24, 2017 at 10:20
  • What makes you think you need to switch to int32 (which doesn't exist)? Commented Mar 24, 2017 at 10:23
  • I don't know what a NZEC error is, but an input of / 1 0 will definitely throw an exception Commented Mar 24, 2017 at 10:24
  • In direction it says that result should be in int32. Commented Mar 24, 2017 at 10:25

2 Answers 2

4

If the result should be an integer, you should use operator.floordiv, i.e. a // b, not a / b 1:

from operator import add, sub, mul, floordiv, mod

op = {'+': add, '-': sub, '*': mul, '/': floordiv, '%': mod}

for i in range(int(input())):
    znak, *nums = input().split()

    print(op[znak](*map(int, nums)))

By the way, the code above does exactly the same as yours, but it's around two times shorter!

How?

  • why check if znak is equal to something with tons of if/else statements, if you could put all the math operators in a dictionary op, and get the operator you need with op[znak]?
  • you're on Python 3, so you can use that nice a, *b = iterable syntax which extracts the first item of iterable into a and puts other items into a list b, which looks super nice
  • each op[znak] is a function that accepts two arguments, so you convert nums to integers with map(int, nums) and then pass them as separate arguments with the asterisk: *map(int, nums)
  • last, but not least, why use this C-style while loop index incrementing if there's the Pythonic way to do it - with range(start, stop, [step])?
  • finally, you don't really need the variable n, so you can plug it into range right away

Pssst, dude, feeling in need of some craziness? Take a look at how you can squeeze all this into two lines:

from operator import*

sum(0for _ in map(print,((lambda znak,*nums:{'+':add,'-':sub,'*':mul,'/':floordiv,'%':mod}[znak](*map(int,nums)))(*input().split())for _ in range(int(input())))))

Or only one line:

sum(0for _ in map(print,((lambda znak,*nums:{'+':lambda a,b:a+b,'-':lambda a,b:a-b,'*':lambda a,b:a*b,'/':lambda a,b:a//b,'%':lambda a,b:a%b}[znak](*map(int,nums)))(*input().split())for _ in range(int(input())))))

These work exactly as the first version. Now, this is just for fun, to show how powerful Python is and how messy it can be. Don't try this at home :D


1 True division vs Floor division: 1 / 10 == 0.1, but 1 // 10 == 0.

Sign up to request clarification or add additional context in comments.

Comments

0

A simple solution: The idea is to split input charter into tokens preserving their orders . in our case tokens are operators and numbers so it very simple using regular expressions. [+-/*] match any of those operators or any number of digits using \d+

n = input()
import re
while n>0:
    expression = input()
    operator,num1,num2 = re.findall('[+-/*]|\d+',expression)
    if operator == '+': print(int(num1) + int (num2))
    if operator == '-': print(int(num1) - int (num2))
    if operator == '*': print(int(num1) * int (num2))
    if operator == '%': print(int(num1) % int (num2))
    if operator == '/': print(int(num1) // int (num2))

    n = n - 1

9 Comments

Still, an integer divided by an integer like a / b will be a floating point number.
@ForceBru fixed
no, it's not: if you divide a number by a floating-point, you can be even more sure you'll get a floating-point as a result.
well, the OP said this in the comments: "In direction it says that result should be in int32."
you're using Python 2.x, but the OP is on Python 3.x
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.