[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: validate string is valid maths

Matthew_WARREN

1/28/2008 4:32:00 PM


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@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/p...



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.
2 Answers

marek.rocki

1/28/2008 5:44:00 PM

0

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_case in ('3++++++8', '3++--*-9', '3++*/-9', '45--/**/+7',
'55/-**+-6**'):
print re.match(re_expression, test_case).groups()

Now you have two problems ;-)

One question: you write "in cases where multiple symbols conflict in
meaning (as '3++--*-9' the earliest valid symbols in the sequence
should be preserved". Aren't '++' the earliest valid symbols
(operation and sign)? It seems, instead, that in your test cases the
sign is always immediately preceding the number. My regexp
accommodates for that.

Regards,
Marek

Gabriel Genellina

1/28/2008 6:31:00 PM

0

impor tOn 28 ene, 14:31, Matthew_WAR...@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/typesma... )

> 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