Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from random import choice, randint
def rb(): return choice([True, False])

def rand(depth=6, t=True):
    if depth == 0:
        if t:
            return "1"
        else:
            return "0"
    else:
        c = choice(["not", "and", "or"])
        if "not" == c:
            return "(not " + rand(depth-1, not t) + ")"
        elif "and" == c:
            if t:
                return "(and " + rand(depth-1, True) + " " + rand(depth-1, True) + ")"
            else:
                if rb():
                    return "(and " + rand(depth-1, rb()) + " " + rand(depth-1, False) + ")"
                else:
                    return "(and " + rand(depth-1, False) + " " + rand(depth-1, rb()) + ")"
        elif "or" == c:
            if not t:
                return "(or " + rand(depth-1, False) + " " + rand(depth-1, False) + ")"
            else:
                if rb():
                    return "(or " + rand(depth-1, rb()) + " " + rand(depth-1, True) + ")"
                else:
                    return "(or " + rand(depth-1, True) + " " + rand(depth-1, rb()) + ")"

if __name__ == '__main__':
    print rand()