validate string is valid maths

M

Matthew_WARREN

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

Steven D'Aprano

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
 
C

Christian Heimes

Steven said:
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 the length of the string s after each
iteration and compare it to the last iteration. When the string hasn't
changed break out of the loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top