# validate string is valid maths

Matthew_WARREN at bnpparibas.com Matthew_WARREN at bnpparibas.com
Tue Jan 29 11:49:16 CET 2008

```It was a very loosely thought out problem, and my Maths isn't good enough
to define 'sane' rules for collapsing the signs/operators to  make a
sensible expression; so take my constraints with a pinch of salt.

I guess a better way of putting it may be - now it has been pointed out
that 8+++++++9 is valid;

Remove the smalles number of symbols such that eval() will always return a
number, and the result is always the same.

....and I havent had a chance to play with the couple of solutions posted
yet, so those criteria may have already been met.

One thing I have discovered is you cant just pass arbitrary valid
(expression wise) numeral/symbol strings to eval and have it work as
expected;

>>> eval('8-038')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
8-038
^
SyntaxError: invalid token

becasue of
>>> eval('8-010')
0

Can I escape the meaning of the leading 0's on an integer?

....and despite my initial claims, there is an overall aim. Still purely
just to play with python though - after someone mentioned Genetic
Algorithms on the list yesterday I thought I'd have a go at a very simple
one.

These long symbol/number strings are the 'genomes/chromosomes' (not certain
on correct terms), the 'genes' are 1234567890+/-*

I'm generating large populations of arbitrary length chromosomes. Using
eval(chromosome) to compute a number. The fittest individuals are the ones
who's genome evaluates closest to 10000000, so in essence I'm evolving
solutions to making the number 10000000 using 1234567890/*-+ (this was
partially inspired by the countdown numbers game discussed here too.).
Trivial and possibly pointless, but shiny enough for me to play with :)

Matt.

Internet
gagsl-py2 at yahoo.com.ar
To
python-list
Sent by:                                                                                             cc
python-list-bounces+matthew.warren=uk.bnpparibas.com@
python.org                                                                                      Subject
Re: validate string is valid maths
28/01/2008 18:30

impor tOn 28 ene, 14:31, Matthew_WAR... at bnpparibas.com 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 cases where multiple symbols conflict in meaning (as '3++--*-9' the
> earliest valid symbols in the sequence should be preserved
>
> so for example,
>
> '3++*/-9'         =     3+-9
> '45--/**/+7'      =     45-+7
> '55/-**+-6**'     =     55/-6

Why not 3++-9, 45--+7? Does it have to be two operators? Why not 3++9
instead? they're the two earliest valid symbols. Can't repeat yourself
then? (I'm trying to understand the rules...)

This approach uses regular expressions. It doesn't follow all your
rules, and tries to get the longest valid expression:

import re

def repl_middle(match):
g = match.group()
if g[0] in '*/':
g0 = g[0]
g = g[1:]
else: g0 = ''
return g0 + g.replace('*','').replace('/','')

def repl_start(match):
g = match.group()
return g.replace('*','').replace('/','')

def dropinvalid(s):
s = re.sub(r'(?<=\d)[+*/-]+(?=\d)', repl_middle, s)
s = re.sub(r'^[+*/-]+', repl_start, s)
s = re.sub(r'[+*/-]+\$', '', s)
return s

cases = [
('3++++++8', '3+8'),
('3++--*-9', '3+-9'),
('3++*/-9', '3+-9'),
('45--/**/+70', '45-+70'),
('55/-**+-6**', '55/-6'),
('55/**6**', '55/6'),
]

for expr, matthew in cases:
print expr, dropinvalid(expr), matthew

> I've tried testing, but I'm not certain wether repeated iterations over a
> dict return different sequences of key,value pairs or wether I'll be
> getting the same (but arbitrary) sequence each time even though they are
> unordered, etc

If the dictionary hasn't changed, you'll get the same sequence each
time (see note (3) in http://docs.python.org/lib/typesmapping.html )

> So for testing, what could I do to guarantee the next iteration over the
> dict will give keys/pairs in a different sequence to last time?

items = dict.items()
random.shuffle(items)
for key,value in items:
...

(Ok, no guarantee, there is only a certain probability that it will be
different each time...)

--
Gabriel Genellina
--
http://mail.python.org/mailman/listinfo/python-list

This message and any attachments (the "message") is
intended solely for the addressees and is confidential.
immediately notify the sender. Any use not in accord with
its purpose, any dissemination or disclosure, either whole
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message.
BNP PARIBAS (and its subsidiaries) shall (will) not
therefore be liable for the message if modified.
Do not print this message unless it is necessary,
consider the environment.

---------------------------------------------

Ce message et toutes les pieces jointes (ci-apres le
"message") sont etablis a l'intention exclusive de ses
destinataires et sont confidentiels. Si vous recevez ce
message par erreur, merci de le detruire et d'en avertir
immediatement l'expediteur. Toute utilisation de ce
message non conforme a sa destination, toute diffusion
ou toute publication, totale ou partielle, est interdite, sauf
autorisation expresse. L'internet ne permettant pas
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce
message, dans l'hypothese ou il aurait ete modifie.
N'imprimez ce message que si necessaire,
pensez a l'environnement.

```