Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用L/R移位、L/R循环、and、XOR和OR有效地解析和计算位字符串表达式?_Python_Parsing_Bitstring - Fatal编程技术网

Python 如何使用L/R移位、L/R循环、and、XOR和OR有效地解析和计算位字符串表达式?

Python 如何使用L/R移位、L/R循环、and、XOR和OR有效地解析和计算位字符串表达式?,python,parsing,bitstring,Python,Parsing,Bitstring,在课堂上,我们刚刚开始学习位字符串翻转,从基本函数开始:LSHIFT、RSHIFT、LCIRC、RCIRC和、XOR或。然后,突然,我们接到了编写python程序的任务,该程序将解析和计算位字符串表达式。虽然我发现手工求解位字符串表达式相当简单,但我没有掌握通过python解析和计算位字符串表达式的有效方法。我已经定义了下面所有必需的函数,这些函数适用于单运算符表达式(即LCIRC 4 0010),但到目前为止,我完全无法解析多运算符表达式(即LCIRC 3 LCIRC 3 0010) 假设我正

在课堂上,我们刚刚开始学习位字符串翻转,从基本函数开始:LSHIFT、RSHIFT、LCIRC、RCIRC和、XOR或。然后,突然,我们接到了编写python程序的任务,该程序将解析和计算位字符串表达式。虽然我发现手工求解位字符串表达式相当简单,但我没有掌握通过python解析和计算位字符串表达式的有效方法。我已经定义了下面所有必需的函数,这些函数适用于单运算符表达式(即LCIRC 4 0010),但到目前为止,我完全无法解析多运算符表达式(即LCIRC 3 LCIRC 3 0010)


假设我正确理解您的问题陈述(第二个示例输出是否应为1000?),我可以想出两种解决问题的方法:

1) 从右向左求值。如果您只考虑单变量函数调用,就像循环移位一样,那么您应该首先简单地计算最里面(最右边)的表达式

2) 使用。这基本上采取如下形式(请原谅伪代码)


显然,我在这段代码中留下了很多漏洞,但毕竟这不是一项任务吗?:)我希望这能帮助您开始思考这是如何工作的。

似乎是递归或

我的简单方法是使用带maxsplit参数的string方法:

def evaluate(s):
    cmd, arg1, arg2 = s.split(None, 2)
    if ' ' in arg2:
        arg2 = evaluate(arg2)
    return command(arg1, arg2) # TODO: call correct function with correct arguments

def command(a1, a2):
    print a1, a2

乌普斯。是的,应该是1000。非常感谢您的帮助。谢谢,但是反向波兰语表示法(后缀)或波兰语表示法(前缀)的唯一问题是不能使用后缀或前缀表示整个表达式。例如,LCIRC 1 00010是前缀,但LCIRC 1 00010&00100不是有效的前缀、中缀或后缀表达式。
>>> LCIRC 1 0010
>>> Your answer is --> 0100
>>> LCIRC 1 LCIRC 1 0010
>>> Your answer is 1000
def handleExpression(str):
    #assuming str is of form of parse
    #if two sided operator
    if(typeof str[0] == number)
        #Find base operator and the two expressions which represent its arguments.
        #^^Format the arguments in the form of parse
        #(You can think of this in terms of parentheses: for every open parenthesis, you need a closing parenthesis.
        #So, you can just keep track of open and close "parentheses" until you reach an operand with a balance of "parentheses")
        operation = #xor, and, etc.
        return operation(handleExpression(leftSide), handleExpression(rightSide))
    else #some kind of shift
        #whatever code
def evaluate(s):
    cmd, arg1, arg2 = s.split(None, 2)
    if ' ' in arg2:
        arg2 = evaluate(arg2)
    return command(arg1, arg2) # TODO: call correct function with correct arguments

def command(a1, a2):
    print a1, a2