Skip to content

Instantly share code, notes, and snippets.

@sevaseva
sevaseva / custom_eval_string_numeric_expression.py
Last active October 28, 2018 06:58
custom eval function in python for a nonempty sting that may contain only '0'..'9', '+', '*'
ORD_OF_ZERO = ord('0')
def _eval(str):
''' Returns an integer, a result of evaluation of a nonempty str
that may contain only '0'..'9', '+', '*'.
Summary of the algorithm:
* .split('+') the string into products (strings),
* then .split('*') each product string into numbers (strings),
* evaluate each number (string) into an integer,
* multiply all numbers within each product to get the numerical product,