Division

i am not sure share your opinions

In Python most of times we have to output 5 instead of 5.0 but 2.3 for 2.3 and u feel bad i think, So i use a technique.

def f(x):
if ‘.0’ in str(x):return int(x)
return x

what about this

def f(x):
if ‘.0’ in str(x) and len(str(x)[str(x).index(’.’):])==2:
return int(x)
return x

What if x is 1.002 or if x is “192.168.0.0”?

1 Like

def f(x):
if ‘.0’ in str(x) and len(str(x)[str(x).index(’.’):])==2:
return int(x)
return x

will this work ?

Unfortunately true, but this is nothing else than bad problem design. Because CG has no tolerance whatsoever on the outputs of classic puzzles and CoC (it does not allow to run a program to parse & check the output, but simply compares two text files), then the floating point outputs should simply always be uniformized to a fixed number of decimals (with a properly specified rounding method).

That said, here is another way to deal with that:

print(int(x) if x.is_integer() else x)
3 Likes

wow , thanks