I've found a problem with some code that I am writing; when I extract the data from my JSON file and put it into POSTFIX order, when encountering functions such as Power, Sum, IF, NOT, it doesn't put them in the correct postfix order. I've been trying to solve this for ages but I can't understand how to solve it. Here are some examples of the JSON file:
sum(3+4*3*2+2,1,2,3)
{
"arguments": [{
"rightArgument": {
"value": "2",
"type": "constant"
},
"leftArgument": {
"rightArgument": {
"rightArgument": {
"value": "2",
"type": "constant"
},
"leftArgument": {
"rightArgument": {
"value": "3",
"type": "constant"
},
"leftArgument": {
"value": "4",
"type": "constant"
},
"type": "operation",
"operator": "*"
},
"type": "operation",
"operator": "*"
},
"leftArgument": {
"value": "3",
"type": "constant"
},
"type": "operation",
"operator": "+"
},
"type": "operation",
"operator": "+"
}, {
"value": "1",
"type": "constant"
}, {
"value": "2",
"type": "constant"
}, {
"value": "3",
"type": "constant"
}],
"name": "sum",
"type": "function"
}
Another example, power(2,3)+not(2=1)
:
{
"rightArgument": {
"arguments": [{
"rightArgument": {
"value": "1",
"type": "constant"
},
"leftArgument": {
"value": "2",
"type": "constant"
},
"type": "operation",
"operator": "="
}],
"name": "not",
"type": "function"
},
"leftArgument": {
"arguments": [{
"value": "2",
"type": "constant"
}, {
"value": "3",
"type": "constant"
}],
"name": "power",
"type": "function"
},
"type": "operation",
"operator": "+"
}
and finally: IF(1,IF(1,2,3),A1)
:
{
"arguments": [{
"value": "1",
"type": "constant"
}, {
"arguments": [{
"value": "1",
"type": "constant"
}, {
"value": "2",
"type": "constant"
}, {
"value": "3",
"type": "constant"
}],
"name": "IF",
"type": "function"
}, {
"cell": "A1",
"value": "",
"type": "cell"
}],
"name": "IF",
"type": "function"
}
As you can see I have to cater for a lot of different formats of formulae
currently my code to store the mathematical problem into a string is this:
def _getCurrentOperator(data):
if data["type"] == "operation":
_getCurrentOperator(data["rightArgument"])
_getCurrentOperator(data["leftArgument"])
valueList.append(data["operator"])
elif data["type"] == "group":
_getCurrentOperator(data["argument"])
elif data["type"] == "function":
if (data["name"] == "pi"):
valueList.append(3.14159265359)
else:
valueList.append(data["name"])
for i in range(len(data["arguments"])):
_getCurrentOperator(data["arguments"][i])
else:
if (data["value"]) == '':
valueList.append(data["cell"])
else:
valueList.append(float(data["value"]))
the input data is a JSON representation of the mathematical problem I believe this is all the code you require to replicate the problem. Currently it doesn't store the functions (power, IF, NOT) in non-postfix order however It does store the values in the right order.
If you want to make it postfix, you have to put the name of the function after the arguments. That's all.
def _getCurrentOperator(data):
if data["type"] == "operation":
_getCurrentOperator(data["rightArgument"])
_getCurrentOperator(data["leftArgument"])
valueList.append(data["operator"]) # here you do it right
...
elif data["type"] == "function":
if (data["name"] == "pi"):
valueList.append(3.14159265359)
else:
for i in range(len(data["arguments"])): # first the arguments...
_getCurrentOperator(data["arguments"][i])
valueList.append(data["name"]) # then the operation
else:
...
Also, you probably shouldn't handle pi
separately. IMHO it's just another function with zero parameters.