validate string is valid maths

Matthew_WARREN at bnpparibas.com Matthew_WARREN at bnpparibas.com
Mon Jan 28 10:10:54 EST 2008


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 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

...that is, unless there is a canonical method for doing this that does
something else instead..


this sounds like homework. It's not. I like making problems up and it's a
slow work day. So, as an aside, there is no real reason I want to do this,
nor other problem to solve, nor other background to what I'm trying to
achieve ;) other than twiddling with python.

Matt.



This message and any attachments (the "message") is
intended solely for the addressees and is confidential. 
If you receive this message in error, please delete it and 
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.



More information about the Python-list mailing list