mirror of
https://github.com/heqin-zhu/algorithm.git
synced 2024-03-22 13:30:46 +08:00
33 lines
586 B
Python
33 lines
586 B
Python
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)))
|