我可以在Python 2.7中编写一个执行符号计算的函数吗?
我目前正在从 Java 过渡到 Python,并承担了尝试创建一个计算器的任务,该计算器可以对中缀表示的数学表达式执行符号运算(不使用像 Sympy 这样的自定义模块)。目前,它的构建接受以空格分隔的字符串,并且只能执行 (、)、+、-、* 和 / 运算符。不幸的是,我无法弄清楚简化符号表达式的基本算法。
例如,给定字符串 '2 * ( ( 9 / 6 ) + 6 * x )',我的程序应该执行以下步骤:
- 2 * ( 1.5 + 6 * x )
- 3 + 12 * x
但我不能让程序在分发 2 时忽略 x。 此外,如何处理 'x * 6 / x' 以便简化后返回 '6'?
编辑:为了澄清,我所说的“符号”是指在执行其余计算时,它将在输出中留下“A”和“f”等字母。
编辑2:我(大部分)完成了代码。如果将来有人偶然发现这篇文章,或者你们中有人好奇的话,我会将其发布在这里。
def reduceExpr(useArray):
# Use Python's native eval() to compute if no letters are detected.
if (not hasLetters(useArray)):
return [calculate(useArray)] # Different from eval() because it returns string version of result
# Base case. Returns useArray if the list size is 1 (i.e., it contains one string).
if (len(useArray) == 1):
return useArray
# Base case. Returns the space-joined elements of useArray as a list with one string.
if (len(useArray) == 3):
return [' '.join(useArray)]
# Checks to see if parentheses are present in the expression & sets.
# Counts number of parentheses & keeps track of first ( found.
parentheses = 0
leftIdx = -1
# This try/except block is essentially an if/else block. Since useArray.index('(') triggers a KeyError
# if it can't find '(' in useArray, the next line is not carried out, and parentheses is not incremented.
try:
leftIdx = useArray.index('(')
parentheses += 1
except Exception:
pass
# If a KeyError was returned, leftIdx = -1 and rightIdx = parentheses = 0.
rightIdx = leftIdx + 1
while (parentheses > 0):
if (useArray[rightIdx] == '('):
parentheses += 1
elif (useArray[rightIdx] == ')'):
parentheses -= 1
rightIdx += 1
# Provided parentheses pair isn't empty, runs contents through again; else, removes the parentheses
if (leftIdx > -1 and rightIdx - leftIdx > 2):
return reduceExpr(useArray[:leftIdx] + [' '.join(['(',reduceExpr(useArray[leftIdx+1:rightIdx-1])[0],')'])] + useArray[rightIdx:])
elif (leftIdx > -1):
return reduceExpr(useArray[:leftIdx] + useArray[rightIdx:])
# If operator is + or -, hold the first two elements and process the rest of the list first
if isAddSub(useArray[1]):
return reduceExpr(useArray[:2] + reduceExpr(useArray[2:]))
# Else, if operator is * or /, process the first 3 elements first, then the rest of the list
elif isMultDiv(useArray[1]):
return reduceExpr(reduceExpr(useArray[:3]) + useArray[3:])
# Just placed this so the compiler wouldn't complain that the function had no return (since this was called by yet another function).
return None
I'm currently transitioning from Java to Python and have taken on the task of trying to create a calculator that can carry out symbolic operations on infix-notated mathematical expressions (without using custom modules like Sympy). Currently, it's built to accept strings that are space delimited and can only carry out the (, ), +, -, *, and / operators. Unfortunately, I can't figure out the basic algorithm for simplifying symbolic expressions.
For example, given the string '2 * ( ( 9 / 6 ) + 6 * x )', my program should carry out the following steps:
- 2 * ( 1.5 + 6 * x )
- 3 + 12 * x
But I can't get the program to ignore the x when distributing the 2. In addition, how can I handle 'x * 6 / x' so it returns '6' after simplification?
EDIT: To clarify, by "symbolic" I meant that it will leave letters like "A" and "f" in the output while carrying out the remaining calculations.
EDIT 2: I (mostly) finished the code. I'm posting it here if anyone stumbles on this post in the future, or if any of you were curious.
def reduceExpr(useArray):
# Use Python's native eval() to compute if no letters are detected.
if (not hasLetters(useArray)):
return [calculate(useArray)] # Different from eval() because it returns string version of result
# Base case. Returns useArray if the list size is 1 (i.e., it contains one string).
if (len(useArray) == 1):
return useArray
# Base case. Returns the space-joined elements of useArray as a list with one string.
if (len(useArray) == 3):
return [' '.join(useArray)]
# Checks to see if parentheses are present in the expression & sets.
# Counts number of parentheses & keeps track of first ( found.
parentheses = 0
leftIdx = -1
# This try/except block is essentially an if/else block. Since useArray.index('(') triggers a KeyError
# if it can't find '(' in useArray, the next line is not carried out, and parentheses is not incremented.
try:
leftIdx = useArray.index('(')
parentheses += 1
except Exception:
pass
# If a KeyError was returned, leftIdx = -1 and rightIdx = parentheses = 0.
rightIdx = leftIdx + 1
while (parentheses > 0):
if (useArray[rightIdx] == '('):
parentheses += 1
elif (useArray[rightIdx] == ')'):
parentheses -= 1
rightIdx += 1
# Provided parentheses pair isn't empty, runs contents through again; else, removes the parentheses
if (leftIdx > -1 and rightIdx - leftIdx > 2):
return reduceExpr(useArray[:leftIdx] + [' '.join(['(',reduceExpr(useArray[leftIdx+1:rightIdx-1])[0],')'])] + useArray[rightIdx:])
elif (leftIdx > -1):
return reduceExpr(useArray[:leftIdx] + useArray[rightIdx:])
# If operator is + or -, hold the first two elements and process the rest of the list first
if isAddSub(useArray[1]):
return reduceExpr(useArray[:2] + reduceExpr(useArray[2:]))
# Else, if operator is * or /, process the first 3 elements first, then the rest of the list
elif isMultDiv(useArray[1]):
return reduceExpr(reduceExpr(useArray[:3]) + useArray[3:])
# Just placed this so the compiler wouldn't complain that the function had no return (since this was called by yet another function).
return None
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在对符号进行操作之前,您需要进行更多处理。您想要获得的形式是叶节点中具有值的操作树。首先,您需要在字符串上运行词法分析器来获取元素 - 尽管如果您总是有空格分隔的元素,那么只需拆分字符串就足够了。然后,您需要使用所需的语法来解析该标记数组。
如果您需要有关语法和解析文本的理论信息,请从这里开始:http://en.wikipedia.org/ wiki/Parsing 如果您需要更实用的内容,请访问 https://github.com/pyparsing/pyparsing (您不必使用 pyparsing 模块本身,但他们的文档有很多有趣的信息)或 http://www.nltk.org/book
来自
2 * ( ( 9 / 6 ) + 6 * x )
,你需要得到这样的树:然后你可以访问每个节点并决定是否要简化它。常量操作将是最简单的消除操作 - 只需计算结果并将“/”节点与 1.5 交换,因为所有子节点都是常量。
有很多策略可以继续,但本质上您需要找到一种方法来遍历树并对其进行修改,直到没有任何内容可以更改为止。
如果您想打印结果,只需再次遍历树并生成描述它的表达式即可。
You need much more processing before you go into operations on symbols. The form you want to get to is a tree of operations with values in the leaf nodes. First you need to do a lexer run on the string to get elements - although if you always have space-separated elements it might be enough to just split the string. Then you need to parse that array of tokens using some grammar you require.
If you need theoretical information about grammars and parsing text, start here: http://en.wikipedia.org/wiki/Parsing If you need something more practical, go to https://github.com/pyparsing/pyparsing (you don't have to use the pyparsing module itself, but their documentation has a lot of interesting info) or http://www.nltk.org/book
From
2 * ( ( 9 / 6 ) + 6 * x )
, you need to get to a tree like this:Then you can visit each node and decide if you want to simplify it. Constant operations will be the simplest ones to eliminate - just compute the result and exchange the "/" node with 1.5 because all children are constants.
There are many strategies to continue, but essentially you need to find a way to go through the tree and modify it until there's nothing left to change.
If you want to print the result then, just walk the tree again and produce an expression which describes it.
如果您在 Python 中解析表达式,则可以考虑表达式的 Python 语法并使用
ast
模块(AST = 抽象语法树)。使用 Python 语法的优点:您不必为此目的创建单独的语言,解析器是内置的,评估器也是内置的。缺点:解析树中有很多您不需要的额外复杂性(您可以通过使用内置的 NodeVisitor 和 NodeTransformer 类来避免其中的一些复杂性)做你的工作)。
下面是一个示例类,它遍历 Python 解析树并执行递归常量折叠(对于二进制操作),向您展示可以相当轻松地完成的操作。
If you are parsing expressions in Python, you might consider Python syntax for the expressions and parse them using the
ast
module (AST = abstract syntax tree).The advantages of using Python syntax: you don't have to make a separate language for the purpose, the parser is built in, and so is the evaluator. Disadvantages: there's quite a lot of extra complexity in the parse tree that you don't need (you can avoid some of it by using the built-in
NodeVisitor
andNodeTransformer
classes to do your work).Here's an example class that walks a Python parse tree and does recursive constant folding (for binary operations), to show you the kind of thing you can do fairly easily.