algorithm-in-python/parser/calculator/genExpr.py

33 lines
586 B
Python
Raw Normal View History

2019-03-15 16:37:02 +08:00
from random import randint
def genOp(li):
return li[randint(0, len(li)-1)]
def genNum(n=20):
return randint(1, n)
def genFactor(n=3):
n = randint(1, n)
ret = [str(genNum())]
for i in range(n):
ret.append(genOp('*/'))
ret.append(str(genNum()))
return ''.join(ret)
def genExpr(n=8):
n = randint(3, n)
ret = [genFactor()]
for i in range(n):
ret.append(genOp('+-'))
ret.append(genFactor())
return ' '.join(ret)
if __name__ == '__main__':
s = genExpr()
print('evaluate "{}" == {}'.format(s, eval(s)))