我目前正在尝试创建一个Python脚本,它将自动生成有效的空格分隔的算术表达式。但是,我得到的示例输出如下:( 32 - 42 / 95 + 24 ( ) ( 53 ) + ) 21
虽然空括号完全可以,但我不能在计算中使用这个自动生成的表达式,因为24和53之间没有运算符,并且结尾21之前的+没有第二个参数。在
我想知道的是,有没有一种方法可以使用python解决方案来解释/修复这些错误?(在有人指出这一点之前,我将首先承认,我在下面发布的代码可能是我推出的最差的代码,并且符合……好吧,几乎没有Python的核心原则。)
import random
parentheses = ['(',')']
ops = ['+','-','*','/'] + parentheses
lines = 0
while lines < 1000:
fname = open('test.txt','a')
expr = []
numExpr = lines
if (numExpr % 2 == 0):
numExpr += 1
isDiv = False # Boolean var, makes sure there's no Div by 0
# isNumber, isParentheses, isOp determine whether next element is a number, parentheses, or operator, respectively
isNumber = random.randint(0,1) == 0 # determines whether to start sequence with number or parentheses
isParentheses = not isNumber
isOp = False
# Counts parentheses to ensure parentheses are matching
numParentheses = 0
while (numExpr > 0 or numParentheses > 0):
if (numExpr < 0 and numParentheses > 0):
isDiv = False
expr.append(')')
numParentheses -= 1
elif (isOp and numParentheses > 0):
rand = random.randint(0,5)
expr.append(ops[rand])
isDiv = (rand == 3) # True if div op was just appended
# Checks to see if ')' was appended
if (rand == 5):
isNumber = False
isOp = True
numParentheses -= 1
# Checks to see if '(' was appended
elif (rand == 4):
isNumber = True
isOp = False
numParentheses += 1
# All other operations go here
else:
isNumber = True
isOp = False
# Didn't add parentheses possibility here in case expression in parentheses somehow reaches 0
elif (isNumber and isDiv):
expr.append(str(random.randint(1,100)))
isDiv = False
isNumber = False
isOp = True
# If a number's up, decides whether to append parentheses or a number
elif (isNumber):
rand = random.randint(0,1)
if (rand == 0):
expr.append(str(random.randint(0,100)))
isNumber = False
isOp = True
elif (rand == 1):
if (numParentheses == 0):
expr.append('(')
numParentheses += 1
else:
rand = random.randint(0,1)
expr.append(parentheses[rand])
if rand == 0:
numParentheses += 1
else:
numParentheses -= 1
isDiv = False
numExpr -= 1
fname.write(' '.join(expr) + '\n')
fname.close()
lines += 1
我在类似的任务中发现了这个线程,即为符号计算的单元测试生成随机表达式。在我的版本中,我包含了一元函数并允许符号是任意字符串,也就是说,可以使用数字或变量名。在
从前面的答案中复制,我只是提出了一些关于概率为
^{pr2}$PROP_PARANTHESIS
的二元运算符(这有点作弊)。二元运算符比一元运算符更常见,因此我也将其留作配置(PROP_BINARY
)。例如:这将产生类似于:
放入
PROP_BINARY = 1.0
并使用把我们带回到输出
实际上,只要Ray Toal的响应是形式上正确的,对于这样一个简单的问题,您不必对每个运算符*进行子类化。我想出了以下代码,它运行得很好:
尽管可以改进它来考虑诸如按零除(目前还没有处理)、通过属性定制所有参数、允许
maxNumbers
参数的任何值等等。在*我所说的“简单问题”是指“生成有效表达式”;如果您添加了任何其他功能(例如,表达式求值),那么Ray的方法将付出代价,因为您可以以更干净的方式定义每个子类的行为。在
编辑(输出):
^{pr2}$是的,你可以用Python的方式生成随机的算术表达式。不过,你需要改变你的方法。不要试图生成一个字符串并计算parens数。而是生成一个随机的表达式树,然后输出它。在
我所说的表达式树是一个类的实例,例如,}。除了
Expression
,子类为Number
,PlusExpression,
MinusExpression, 'TimesExpression
,DivideExpression
,和{Number
之外,每个都有Expression
类型的字段。给每个人一个合适的__str__
方法。生成一些随机表达式对象并打印“根”你能把它从这里拿走还是让我把它编码?在
附录:一些启动程序代码示例。不会生成随机表达式(还没有?)但这可以补充。。。。在
**附录2**
回到Python真的很有趣。我忍不住实现了随机表达式生成器。它是基于上面的代码构建的。很抱歉硬编码!!在
^{pr2}$以下是我得到的输出:
一点都不漂亮。我想这会让很多家长感到厌烦。也许改变在括号表达式和二进制表达式之间选择的概率可能会很好。。。。在
相关问题 更多 >
编程相关推荐