python.org
Subject
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'
>
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 test_cas
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 turn
Subject
Re:
validate string is valid maths
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
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
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 multipl