Re: validate string is valid maths

2008-01-29 Thread Matthew_WARREN
Re: validate string is valid maths 28/01/2008 18:30

Re: validate string is valid maths

2008-01-28 Thread Christian Heimes
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

validate string is valid maths

2008-01-28 Thread Matthew_WARREN
Hi pythoners. I am generating strings of length n, randomly from the symbols +-/*0123456789 What would be the 'sensible' way of transforming the string, for example changing '3++8' into 3+8 or '3++--*-9' into '3+-9' such that eval(string) will always return a number? in cases where

Re: validate string is valid maths

2008-01-28 Thread Steven D'Aprano
On Mon, 28 Jan 2008 15:10:54 +, Matthew_WARREN wrote: Hi pythoners. I am generating strings of length n, randomly from the symbols +-/*0123456789 What would be the 'sensible' way of transforming the string, for example changing '3++8' into 3+8 That's easy: replace pairs of +

Re: validate string is valid maths

2008-01-28 Thread Remco Gerlich
Hi, It seems that for every group of 2 or more +-/* signs, one of the following holds: - The group starts with '-', everything after it should be dropped, otherwise - The second character is '-', everything after it should be dropped, otherwise - Drop everything after the first. That should

Re: validate string is valid maths

2008-01-28 Thread marek . rocki
I decided to play with it a little bit, but take a different approach than Steven. This seems actually one of the problems where regexp might be a good solution. import re re_signednumber = r'([-+]?\d+)' re_expression = '%s(?:([-+/*])[-+/*]*?%s)*' % (re_signednumber, re_signednumber) for

Re: validate string is valid maths

2008-01-28 Thread Gabriel Genellina
impor tOn 28 ene, 14:31, [EMAIL PROTECTED] wrote: What would be the 'sensible' way of transforming the string, for example changing '3++8' into 3+8 or '3++--*-9' into '3+-9' such that eval(string) will always return a number? '3++8' is already a valid expresion, like '3++---9' in

Re: validate string is valid maths

2008-01-28 Thread Matthew_WARREN
Re: validate string is valid maths 28/01/2008 15:43