absoluteCount = abs(x)
typeCount = type(x)
roundCount = round(absoluteCount)
if typeCount and absoluteCount - roundCount == 0:
If the objective was simplicity, it is lost on me; and, probably many learners, too.
A quick solution can be got using
from math import floor
return x - floor(x) == 0
We don’t need to worry about the signs. The floor of a negative number is still negative.
The converse is,
return x - floor(x) > 0
floor always moves leftward on the number line.
ceil always moves to the right.
round moves both directions but we still do not need
return x - round(x) == 0 # is integer
return x - round(x) != 0 # is float
ceil is pretty much the same…
from math import ceil
return ceil(x) - x == 0
The thing about this lesson I believe the author wanted was for learners to do a little research to find as many different ways to accomplish this task as one could.
Take for instance exception handling. This one leverages the fact that floats cannot be interpreted as integer.
return bin(x) and True
It smacks of using
type() but this will work…
return isinstance(x, int)
Cheap and dirty, anyway. My favorite is,
return not x % 1
No functions at work, at all. Just the logical
not and the modulo operator. When the reader sees the
not they know immediately that the return value is
In my own view, this is not the naive solution and very rarely ever surfaces. I lean towards doubt when anyone posts this as their solution.
The simple truth (as I see it) is that a great many learners simply breeze through the lessons and not much sticks. When they get to the Practice Makes Perfect module it shows.