Question: Please help solve this in Python!! You are given an input file with multiple pairs of input lines. The first line of each pair is

Please help solve this in Python!!

You are given an input file with multiple pairs of input lines. The first line of each pair is a tree given as a predecessor array. The second line is the value at the corresponding node. Values at leaf nodes (nodes with no children) are integers. At non-leaf nodes, the two possible values are + or .

The tree represents an arithmetic expression where the value at a non-leaf node u is the sum of values at the children of u in the case of +, or the product of values at the children of u in the case of .

You need to calculate the value at each node and output the calculated value at the root. The tree is not constrained to be binary.

Input format:

Input consists of m pairs of lines of comma separated values, so 2m lines in total. The first line is each pair is a comma separated list of integers representing a tree in predecessor array format where 1 represents null.

The second line in each pair is a comma separated list of integers and the symbols + and . The ith item on the list is the value or operator at the ith node in the tree.

For example:

-1,0,0,0,1,1

+,*,2,3,0,7

2,0,-1,0

+,3,*,3

Output format:

For each pair of input lines, output a line containing the value calculated at the root of the tree.

For the example input above, output would be:

5

6

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!