Skip to content

Instantly share code, notes, and snippets.

@markomanninen
Created July 26, 2017 08:08
Show Gist options
  • Save markomanninen/62007c9642ae1b6e1c813a047fc3d609 to your computer and use it in GitHub Desktop.
Save markomanninen/62007c9642ae1b6e1c813a047fc3d609 to your computer and use it in GitHub Desktop.
function to check if given input is a natural number. with an additional parameter (function) limit of the number can be set
def isN(x, func=None):
try:
val = int(x)
return (len(str(val)) is len(str(x))) and \
(func(val) if callable(func) else True)
except ValueError:
return False
print(
isN(10, (lambda x: -10 <= x <= 10)))
print(
isN(1.0, (lambda x: -10 <= x <= 10)))
print(
all(map(lambda x: isN(x), [1, 2, -1, -2, "1", 0, +3, -3])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment