Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions