samedi 27 février 2021

Inserting parentheses at random place?

stackoverflow community! I am once again asking for your help...

I have made a random equation generator that generates a string like this:

def strmaker():
    picker = random.randint(2, 5)
    if picker == 5:
        return "1" + random.choice(RandomSymbols) + "2" + random.choice(RandomSymbols) + "3" + random.choice(
            RandomSymbols) + "4" + random.choice(RandomSymbols) + "5"
    elif picker == 4:
        return "1" + random.choice(RandomSymbols) + "2" + random.choice(RandomSymbols) + "3" + random.choice(
            RandomSymbols) + "4"
    elif picker == 3:
        return "1" + random.choice(RandomSymbols) + "2" + random.choice(RandomSymbols) + "3"
    else: return "1" + random.choice(RandomSymbols) + "2"

The typical outcome of this would look like this:

1-2*3-4

So basically what i wanna do is: Make some code/function that inserts one or multiple sets of parentheses at a random spot.

Like this!

(1-2)*3-4

Or this!

(1-2)*(3-4)

Or...

(1-2*3)-4

I think you get it at this point. What would be the simplest way to do this?




Aucun commentaire:

Enregistrer un commentaire