Пример #1
0
def randPoly(variables, depth, symbol):
    if depth == 1:
        poly = Polynomial()
        if symbol == "*":
            poly.poly = ["*"]
        else:
            poly.poly = ["+"]

        varsUsed = set()
        for i in range(2):
            if random() < 0.5:
                poly.poly.append(100 * random() - 50)
            else:
                var = choice(variables)
                varsUsed.add(var)
                poly.poly.append(var)

        poly.vars = varsUsed
        return poly
    else:
        poly = Polynomial()
        poly.poly = [symbol]
        varsUsed = set()
        for i in range(randint(2, 5)):
            if symbol == "*":
                newPoly = randPoly(variables, depth - 1, "+")
            elif symbol == "+":
                newPoly = randPoly(variables, depth - 1, "*")

            poly.poly.append(newPoly)
            varsUsed = varsUsed | newPoly.vars

        poly.vars = varsUsed
        return poly