validate string is valid maths

Matthew_WARREN at bnpparibas.com Matthew_WARREN at bnpparibas.com
Mon Jan 28 11:31:50 EST 2008


Ok, I was thinking along the same lines myself, replacing ++ etc.. until no
more replacements are made.

I hadnt considered creating a table of pairs and replacements though, or
realised that the same replace method would work in that case. handy :)

The dictionary is unordered; would differences in the order of replacements
being made effect the final outcome?

IE

*/*

replaced in order of */ then /* would give (using table below)

*/ => **
/* => **

But in the order /* then */

*/*


/* => */
*/ => *

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

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?


...actually, whenever I iterate over the dict say with for k,v in n.items()
I get exactly the same sequence each time.  For once I want to exploit the
dict's unorderedness and it's decided its going to be all ordered...

Matt.




                                                                                                                   
           Internet                                                                                                
           steve at REMOVE-THIS-cybersource.com.au                                                                    
                                                                                                                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 15:43                                                                                        
                                                                                                                   
                                                                                                                   
                                                                                                                   
                                                                                                                   
                                                                                                                   
                                                                                                                   




On Mon, 28 Jan 2008 15:10:54 +0000, 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 + into a single +, repeatedly until there's
nothing left to replace. And so forth. Here's an untested function. It's
probably not even close to optimal, but for playing around it is probably
fine.

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




> 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

You have four symbols, so there are just 4*4=16 sets of two symbols.
Probably the easiest way is to just list them and their replacements out
in a table:

table = {"++": "+", "+-": "-", "+*": "+", "+/": "+",
    "-+": "-", "--": "+", "-*": "-", "-/": "-",
    "*+": "*", "**": "*", "*/": "*",
    "/+": "/", "/*": "/", "//": "/", }

Notice that both *- and /- don't get changed.



# Untested.
def rationalise_signs(s):
    prev = ''
    while s != prev:
        prev = s
        for key, value in table.items():
            s = s.replace(key, value)
    return s



--
Steven
--
http://mail.python.org/mailman/listinfo/python-list



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