Question

Please submit only Python source code. 1. Arithmetic trees 50 marks You are given an input file with multiple pairs of input

0 0
Add a comment Improve this question Transcribed image text
Answer #1

m = int(input())

def solve(tree,op,idx):
   # print(idx)
   if op[idx] not in ['*','+']:
       return int(op[idx])

   if op[idx] == '+':
       # for addition, 0 won't affect the addition
       ans = 0
   elif op[idx] == '*':
       # for multiplication, 1 won't affect the multiplication
       ans = 1  
   for i in range(len(tree[idx])):
       # print(idx,i)
       if op[idx] == '+':
           ans = ans + solve(tree,op,tree[idx][i])
       elif op[idx] == '*':
           ans = ans * solve(tree,op,tree[idx][i])

   # print(ans)
   return ans

for i in range(m):
   tree = list()
   a = input().split(',')
   b = input().split(',')
   a = [int(x) for x in a]

   for j in range(len(a)):
       tree.append(list())

   root = 0
   for j in range(len(a)):
       if a[j] is not -1:
           tree[a[j]].append(j)
       else:
           root = j
   # print(tree)
   print("answer:" ,str(solve(tree,b,root)))

Add a comment
Know the answer?
Add Answer to:
Please submit only Python source code. 1. Arithmetic trees 50 marks You are given an input file with multiple pairs of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi can anyone help me with this question? Please use python when you do it. THANKS...

    Hi can anyone help me with this question? Please use python when you do it. THANKS 1. Arithmetic trees 50 marks 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...

  • plz use python to answer it, thanks in advance 3. Tree and back arcs in a DFS 40 Marks For a given set of digraphs, write a program that performs DFS on each digraph starting at node 0 and prints...

    plz use python to answer it, thanks in advance 3. Tree and back arcs in a DFS 40 Marks For a given set of digraphs, write a program that performs DFS on each digraph starting at node 0 and prints out the total number of tree arcs and back arcs resulting from the traversal. Use our standard convention that when there is a choice of white or grey nodes, the one with the lowest index should be chosen. Input format:...

  • Hierarchical structures: Write a java class to implement a Multi-way tree, with an appropriate constructor and...

    Hierarchical structures: Write a java class to implement a Multi-way tree, with an appropriate constructor and methods for the following:             Methods to find and insert on the tree.             Method to return the height of the tree. Methods to output the values from this tree in any one of the following ways:             Pre-order:        Left to right order of siblings             Post-order:      Right to left order of siblings             Level order:    Left to right order of siblings             Method to store the tree to a file per the...

  • Sc Python 1 Task 2 3 Consider a binary tree of N vertices 4 such that children of node K are 2* K + 1. Vertex 1...

    Sc Python 1 Task 2 3 Consider a binary tree of N vertices 4 such that children of node K are 2* K + 1. Vertex 1 is the root Kand 2 of the tree and each node has an integer value associated with it. Such a tree may be represented as an array of N integers by writing down values from consecutive nodes For example, the tree below 8 Test might be represented as an array o A node...

  • Code to be written in C++: Initially, you will be given symbols printed in a preorder...

    Code to be written in C++: Initially, you will be given symbols printed in a preorder traversal of a boolean expression tree. The internal nodes of the tree will contain one of the following operators: & (and), | (or), ^ (exclusive-or), or ! (not). The nodes containing the first three operators will have two children, while the nodes containing the ! operator will contain only a left child. The leaves of the tree will contain an operand - either f...

  • Would appreciate the answer in the Java coding language please and thank you! 10d 10h left...

    Would appreciate the answer in the Java coding language please and thank you! 10d 10h left Java 7 1. Check the Structure Autocomplete Ready 1 > import java.io.*;... 10 ALL A binary tree uses a multi-node data structure where each node may have 0 to 2 child nodes, and has one stored value, its node number in this case. A tree may either be: 11 class Result { * Complete the 'isValid' function below. • An empty tree, the root...

  • C language huffman This exercise will familiarize you with linked lists, which you will need for...

    C language huffman This exercise will familiarize you with linked lists, which you will need for a subsequent programming Getting Started assignment Overview Requirements Getting Started Submit Start by getting the files. Type 264get hw13 and then cd hw13 from bash. Pre-tester You will get the following files: Q&A Updates 1. huffman.h: An empty header file, you have to define your own functions in this homework. 2. huffman.c: An empty c file, you have to define your own functions in...

  • Java code COCONUT KELAPA4 4A. Input Standard input Output Standard output Topic Array & Array Processing...

    Java code COCONUT KELAPA4 4A. Input Standard input Output Standard output Topic Array & Array Processing Problem Description Ali Wali owns a coconut plantation and a number of monkeys for bringing down coconuts. Every day, Ali would record the number of coconuts brought down by his monkeys. At the end of a certain time period, he would determine from the data recorded, the lowest and the highest number of coconuts as well as their number of occurrences. Write a program...

  • Python Please, If possible, please continue with/ use code already given File Commands: u/a Write a...

    Python Please, If possible, please continue with/ use code already given File Commands: u/a Write a function named, file_commands, that takes the name of a file as a parameter. The function processes the contents the file as follows: For file lines that begin with the letter 'a', calculate & print the integer average of the numbers on the line. For file lines that begin with the letter 'u', print the upper case format for each word following the u. Sample...

  • Summary You will write an application to build a tree structure called Trie for a dictionary...

    Summary You will write an application to build a tree structure called Trie for a dictionary of English words, and use the Trie to generate completion lists for string searches. Trie Structure A Trie is a general tree, in that each node can have any number of children. It is used to store a dictionary (list) of words that can be searched on, in a manner that allows for efficient generation of completion lists. The word list is originally stored...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT