Created
July 26, 2017 08:08
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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