Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- import timeit
- def is_int_trying(string:str):
- try:
- return float(string).is_integer()
- except ValueError:
- return False
- pattern = re.compile(r"([\+\-]?\d+[.[0]]?)")
- def is_int_re(string:str):
- return re.match(pattern, string)
- cases = {
- "0": True, "-0": True, "+0": True,
- "1": True, "-1": True, "+1": True,
- "0.1": False, "-0.1": False, "+0.1": False,
- "1.1": False, "-1.1": False, "+1.1": False,
- "0.000000000000000000001": False,
- "-0.000000000000000000001": False,
- "+0.000000000000000000001": False,
- "⁰12345": False, "-⁰12345": False, "+⁰12345": False
- }
- for case, isint in cases.items():
- for checker in (is_int_trying, is_int_re):
- print("func: {}, val: {}\n correct: {}\n time: ".format(
- checker.__name__, case, checker(case)==isint
- ), end="")
- print(timeit.timeit(
- "checker(case)==isint",
- globals=globals(),
- number=100000
- ))
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement