validate string is valid maths
Christian Heimes
lists at cheimes.de
Mon Jan 28 10:50:36 EST 2008
Steven D'Aprano wrote:
> def rationalise_signs(s):
> while "++" in s or "+-" in s or "-+" in s or "--" in s:
> s = s.replace("++", "+")
> s = s.replace("--", "+")
> s = s.replace("+-", "-")
> s = s.replace("-+", "-")
> return s
I assume it's faster to check the length of the string s after each
iteration and compare it to the last iteration. When the string hasn't
changed break out of the loop.
More information about the Python-list
mailing list