Calculating lists bottom up
up vote
0
down vote
favorite
I'm trying calculate (using operators) the innermost list then the second innermost list etc. until there is no more lists. I'm also trying to do this so that it should be able to calculate the list no matter how many nested lists that's in it.
import operator
lst = [['x1', '*', ['x2', '*', 'x3']], '-', 'x3']
dictionary = {'x1': 4, 'x2': 5, 'x3': 7}
operator_dictionary = {"+": operator.add, "-": operator.sub, "*":
operator.mul, "/": operator.truediv}
def BINARYEXPR(program, dictionary):
for i in range(len(program)):
if isinstance(program[i],list):
return BINARYEXPR(program[i],dictionary)
operator = operator_dictionary[program[1]]
operand1 = dictionary[program[0]]
print("operand1: ", operand1)
operand2 = dictionary[program[2]]
print("operand2: ", operand2)
return operator(operand1,operand2)
print (BINARYEXPR(lst,dictionary))
So what I wanted to do here was to first calculate x2*x3 (5*7) which should give us 35, then calculate x1*35 (4*35) which should give us 140 and then finally take 140 - x3 (140-7) which should return 133. But instead I only managed to calculate the innermost list and hit return operator(operand1,operand2) which ends the function.
So what I'm stuck at is the recursion as I can't seem to figure out how to move on to the second innermost list whenever the innermost list has been calculated.
python list recursion
add a comment |
up vote
0
down vote
favorite
I'm trying calculate (using operators) the innermost list then the second innermost list etc. until there is no more lists. I'm also trying to do this so that it should be able to calculate the list no matter how many nested lists that's in it.
import operator
lst = [['x1', '*', ['x2', '*', 'x3']], '-', 'x3']
dictionary = {'x1': 4, 'x2': 5, 'x3': 7}
operator_dictionary = {"+": operator.add, "-": operator.sub, "*":
operator.mul, "/": operator.truediv}
def BINARYEXPR(program, dictionary):
for i in range(len(program)):
if isinstance(program[i],list):
return BINARYEXPR(program[i],dictionary)
operator = operator_dictionary[program[1]]
operand1 = dictionary[program[0]]
print("operand1: ", operand1)
operand2 = dictionary[program[2]]
print("operand2: ", operand2)
return operator(operand1,operand2)
print (BINARYEXPR(lst,dictionary))
So what I wanted to do here was to first calculate x2*x3 (5*7) which should give us 35, then calculate x1*35 (4*35) which should give us 140 and then finally take 140 - x3 (140-7) which should return 133. But instead I only managed to calculate the innermost list and hit return operator(operand1,operand2) which ends the function.
So what I'm stuck at is the recursion as I can't seem to figure out how to move on to the second innermost list whenever the innermost list has been calculated.
python list recursion
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying calculate (using operators) the innermost list then the second innermost list etc. until there is no more lists. I'm also trying to do this so that it should be able to calculate the list no matter how many nested lists that's in it.
import operator
lst = [['x1', '*', ['x2', '*', 'x3']], '-', 'x3']
dictionary = {'x1': 4, 'x2': 5, 'x3': 7}
operator_dictionary = {"+": operator.add, "-": operator.sub, "*":
operator.mul, "/": operator.truediv}
def BINARYEXPR(program, dictionary):
for i in range(len(program)):
if isinstance(program[i],list):
return BINARYEXPR(program[i],dictionary)
operator = operator_dictionary[program[1]]
operand1 = dictionary[program[0]]
print("operand1: ", operand1)
operand2 = dictionary[program[2]]
print("operand2: ", operand2)
return operator(operand1,operand2)
print (BINARYEXPR(lst,dictionary))
So what I wanted to do here was to first calculate x2*x3 (5*7) which should give us 35, then calculate x1*35 (4*35) which should give us 140 and then finally take 140 - x3 (140-7) which should return 133. But instead I only managed to calculate the innermost list and hit return operator(operand1,operand2) which ends the function.
So what I'm stuck at is the recursion as I can't seem to figure out how to move on to the second innermost list whenever the innermost list has been calculated.
python list recursion
I'm trying calculate (using operators) the innermost list then the second innermost list etc. until there is no more lists. I'm also trying to do this so that it should be able to calculate the list no matter how many nested lists that's in it.
import operator
lst = [['x1', '*', ['x2', '*', 'x3']], '-', 'x3']
dictionary = {'x1': 4, 'x2': 5, 'x3': 7}
operator_dictionary = {"+": operator.add, "-": operator.sub, "*":
operator.mul, "/": operator.truediv}
def BINARYEXPR(program, dictionary):
for i in range(len(program)):
if isinstance(program[i],list):
return BINARYEXPR(program[i],dictionary)
operator = operator_dictionary[program[1]]
operand1 = dictionary[program[0]]
print("operand1: ", operand1)
operand2 = dictionary[program[2]]
print("operand2: ", operand2)
return operator(operand1,operand2)
print (BINARYEXPR(lst,dictionary))
So what I wanted to do here was to first calculate x2*x3 (5*7) which should give us 35, then calculate x1*35 (4*35) which should give us 140 and then finally take 140 - x3 (140-7) which should return 133. But instead I only managed to calculate the innermost list and hit return operator(operand1,operand2) which ends the function.
So what I'm stuck at is the recursion as I can't seem to figure out how to move on to the second innermost list whenever the innermost list has been calculated.
python list recursion
python list recursion
asked Nov 19 at 20:56
Programming_Zeus
33
33
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
A recursive function as follows should do the job:
import operator
lst = [['x1', '*', ['x2', '*', 'x3']], '-', 'x3']
dictionary = {'x1': 4, 'x2': 5, 'x3': 7}
operator_dictionary = {"+": operator.add, "-": operator.sub, "*":
operator.mul, "/": operator.truediv}
def calc(lst):
if type(lst) == str:
return dictionary[lst]
if len(lst) != 3:
raise ValueError("Incorrect expression: {}".format(lst))
op = operator_dictionary[lst[1]]
return op(calc(lst[0]), calc(lst[2]))
Since you are using infix notation, each expression has three components: expression, operator, expression. The function works by assuming the 0th and 2nd elements are operands and the 1st element is the operator. We calculate the sub-expressions recursively, and then apply the operation. Also, if at any point our function receives a list with length different than 3, we throw since this cannot be a well-formed expression.
I don't think giving the user a completely different solution is an appropriate answer to their question.
– ritlew
Nov 19 at 21:09
@ritlew What do you mean by a completely different answer? The only main difference between this one and the one OP originally written is that they have forgotten the recursive call to the function, which I provide.
– eozd
Nov 19 at 21:11
You didn't fix the users code, you provided your own to accomplish the same task that the user was trying to code for themselves. Your code looks nothing like OP's.
– ritlew
Nov 19 at 21:15
Great solution, thank you!
– Programming_Zeus
Nov 19 at 21:26
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
A recursive function as follows should do the job:
import operator
lst = [['x1', '*', ['x2', '*', 'x3']], '-', 'x3']
dictionary = {'x1': 4, 'x2': 5, 'x3': 7}
operator_dictionary = {"+": operator.add, "-": operator.sub, "*":
operator.mul, "/": operator.truediv}
def calc(lst):
if type(lst) == str:
return dictionary[lst]
if len(lst) != 3:
raise ValueError("Incorrect expression: {}".format(lst))
op = operator_dictionary[lst[1]]
return op(calc(lst[0]), calc(lst[2]))
Since you are using infix notation, each expression has three components: expression, operator, expression. The function works by assuming the 0th and 2nd elements are operands and the 1st element is the operator. We calculate the sub-expressions recursively, and then apply the operation. Also, if at any point our function receives a list with length different than 3, we throw since this cannot be a well-formed expression.
I don't think giving the user a completely different solution is an appropriate answer to their question.
– ritlew
Nov 19 at 21:09
@ritlew What do you mean by a completely different answer? The only main difference between this one and the one OP originally written is that they have forgotten the recursive call to the function, which I provide.
– eozd
Nov 19 at 21:11
You didn't fix the users code, you provided your own to accomplish the same task that the user was trying to code for themselves. Your code looks nothing like OP's.
– ritlew
Nov 19 at 21:15
Great solution, thank you!
– Programming_Zeus
Nov 19 at 21:26
add a comment |
up vote
1
down vote
accepted
A recursive function as follows should do the job:
import operator
lst = [['x1', '*', ['x2', '*', 'x3']], '-', 'x3']
dictionary = {'x1': 4, 'x2': 5, 'x3': 7}
operator_dictionary = {"+": operator.add, "-": operator.sub, "*":
operator.mul, "/": operator.truediv}
def calc(lst):
if type(lst) == str:
return dictionary[lst]
if len(lst) != 3:
raise ValueError("Incorrect expression: {}".format(lst))
op = operator_dictionary[lst[1]]
return op(calc(lst[0]), calc(lst[2]))
Since you are using infix notation, each expression has three components: expression, operator, expression. The function works by assuming the 0th and 2nd elements are operands and the 1st element is the operator. We calculate the sub-expressions recursively, and then apply the operation. Also, if at any point our function receives a list with length different than 3, we throw since this cannot be a well-formed expression.
I don't think giving the user a completely different solution is an appropriate answer to their question.
– ritlew
Nov 19 at 21:09
@ritlew What do you mean by a completely different answer? The only main difference between this one and the one OP originally written is that they have forgotten the recursive call to the function, which I provide.
– eozd
Nov 19 at 21:11
You didn't fix the users code, you provided your own to accomplish the same task that the user was trying to code for themselves. Your code looks nothing like OP's.
– ritlew
Nov 19 at 21:15
Great solution, thank you!
– Programming_Zeus
Nov 19 at 21:26
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
A recursive function as follows should do the job:
import operator
lst = [['x1', '*', ['x2', '*', 'x3']], '-', 'x3']
dictionary = {'x1': 4, 'x2': 5, 'x3': 7}
operator_dictionary = {"+": operator.add, "-": operator.sub, "*":
operator.mul, "/": operator.truediv}
def calc(lst):
if type(lst) == str:
return dictionary[lst]
if len(lst) != 3:
raise ValueError("Incorrect expression: {}".format(lst))
op = operator_dictionary[lst[1]]
return op(calc(lst[0]), calc(lst[2]))
Since you are using infix notation, each expression has three components: expression, operator, expression. The function works by assuming the 0th and 2nd elements are operands and the 1st element is the operator. We calculate the sub-expressions recursively, and then apply the operation. Also, if at any point our function receives a list with length different than 3, we throw since this cannot be a well-formed expression.
A recursive function as follows should do the job:
import operator
lst = [['x1', '*', ['x2', '*', 'x3']], '-', 'x3']
dictionary = {'x1': 4, 'x2': 5, 'x3': 7}
operator_dictionary = {"+": operator.add, "-": operator.sub, "*":
operator.mul, "/": operator.truediv}
def calc(lst):
if type(lst) == str:
return dictionary[lst]
if len(lst) != 3:
raise ValueError("Incorrect expression: {}".format(lst))
op = operator_dictionary[lst[1]]
return op(calc(lst[0]), calc(lst[2]))
Since you are using infix notation, each expression has three components: expression, operator, expression. The function works by assuming the 0th and 2nd elements are operands and the 1st element is the operator. We calculate the sub-expressions recursively, and then apply the operation. Also, if at any point our function receives a list with length different than 3, we throw since this cannot be a well-formed expression.
answered Nov 19 at 21:07
eozd
8941515
8941515
I don't think giving the user a completely different solution is an appropriate answer to their question.
– ritlew
Nov 19 at 21:09
@ritlew What do you mean by a completely different answer? The only main difference between this one and the one OP originally written is that they have forgotten the recursive call to the function, which I provide.
– eozd
Nov 19 at 21:11
You didn't fix the users code, you provided your own to accomplish the same task that the user was trying to code for themselves. Your code looks nothing like OP's.
– ritlew
Nov 19 at 21:15
Great solution, thank you!
– Programming_Zeus
Nov 19 at 21:26
add a comment |
I don't think giving the user a completely different solution is an appropriate answer to their question.
– ritlew
Nov 19 at 21:09
@ritlew What do you mean by a completely different answer? The only main difference between this one and the one OP originally written is that they have forgotten the recursive call to the function, which I provide.
– eozd
Nov 19 at 21:11
You didn't fix the users code, you provided your own to accomplish the same task that the user was trying to code for themselves. Your code looks nothing like OP's.
– ritlew
Nov 19 at 21:15
Great solution, thank you!
– Programming_Zeus
Nov 19 at 21:26
I don't think giving the user a completely different solution is an appropriate answer to their question.
– ritlew
Nov 19 at 21:09
I don't think giving the user a completely different solution is an appropriate answer to their question.
– ritlew
Nov 19 at 21:09
@ritlew What do you mean by a completely different answer? The only main difference between this one and the one OP originally written is that they have forgotten the recursive call to the function, which I provide.
– eozd
Nov 19 at 21:11
@ritlew What do you mean by a completely different answer? The only main difference between this one and the one OP originally written is that they have forgotten the recursive call to the function, which I provide.
– eozd
Nov 19 at 21:11
You didn't fix the users code, you provided your own to accomplish the same task that the user was trying to code for themselves. Your code looks nothing like OP's.
– ritlew
Nov 19 at 21:15
You didn't fix the users code, you provided your own to accomplish the same task that the user was trying to code for themselves. Your code looks nothing like OP's.
– ritlew
Nov 19 at 21:15
Great solution, thank you!
– Programming_Zeus
Nov 19 at 21:26
Great solution, thank you!
– Programming_Zeus
Nov 19 at 21:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382502%2fcalculating-lists-bottom-up%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown