diff options
Diffstat (limited to 'python')
-rw-r--r-- | python/util.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/python/util.py b/python/util.py index 3101edf..390b831 100644 --- a/python/util.py +++ b/python/util.py @@ -12,6 +12,28 @@ def has_token_sequence(code, sequence): return True return False +def almost_equal(a, b, prec=1): + """ Compares values a and b using at most <code>prec</code> decimal values. """ + return int(a*10**prec) == int(b*10**prec) + +def string_almost_equal(s, a, prec=1): + """ Searches string s for a value that is almost equal to a. + + Args: + s (str): string to search + a (float): value to find in string + + Returns: + bool: True if successful, else False + """ + for v in s.split(): + try: + if almost_equal(float(v), a, prec): + return True + except: + pass + return False + if __name__ == '__main__': print(has_token_sequence('x + y >= 0', ['>=', '0'])) print(has_token_sequence('x + y > 0', ['>=', '0'])) |