Question: Question 6 In this question you will become a translator. We have some communities who have arrived from the Andromeda galaxy and they can't understand


Question 6 In this question you will become a translator. We have some communities who have arrived from the Andromeda galaxy and they can't understand our mathematical notations. They specifically have trouble understanding the order of operations and what the operations do. We need a function that will translate our mathematical notations into theirs. Thankfully each of them provided templates for the notation they use to make it easier for us to translate. The way they structure their formulas is that they prioritize the execution of the formulas from right to left. For example: If an Andromedan has a formula like, 3 + 5 - 3 * 3 + 2, regardless of what the operations are, they will group it as, (3 + (5 - (3 * (3 + 2)))). You are given two main things, the first is a list that contains the formula in our mathematical notation, each element in the list represents an element in the mathematical notation. For example if we have 3 + 5 - 3* 3 + 2 this will be represented as [3, +, 5, 7, 3, '*', 3, '+', 2). Second resource you are given will be a templates dictionary that maps each of the operations to string templates. For example "+' might be mapped to the string template 'add({0}, {1})' where '{0}' and '{1}' are operands that should be replaced with the operands in the regular mathematical notation. So in this case ' {0}' should be replaced with 3 and '{1}' should be replaced with everything else, namely '(5 - (3 * (3 + 2))) but with its operations translated as well. See the examples in the doctests for more information. You need to return the translated equation in a string format. Restrictions 1. You should use recursion to solve the problem. 2. No loops or map operations are allowed. 3. Do input validation Note: For this question ONLY, you are allowed to use list comprehension inside all() function for input validation. You cannot use other types of loop to make assertions. def build_repr (operations, templates): Build the representation of the series of operations with the Given templates. The operations are prioritized from right to left. Restrictions: You should use recursion. You should do input validation. Parameters: operations (list(str)): List of numbers and operations to be executed on them Returns: (str): String representation of the operations >>> build_repr([0, 't', 1, '-', 3], {'+': '({0}, +, {1}}', '-': '({0}, -, {1})'}) '(0, +, (1, -, 3))' >>> build_repr([0, 't', 1, '-', 3], I {'+': '({0}, add, {1})', -':'({0}, minus, {1})'}) '(0, add, (1, minus, 3))' >>> build_repr([0, 't', 1, '-', 3], 1 {'+': '({0}, add, {1}}', 'L':'({0}, minus, {1})'}) '(0, add, (1, minus, 3))' Question 6 In this question you will become a translator. We have some communities who have arrived from the Andromeda galaxy and they can't understand our mathematical notations. They specifically have trouble understanding the order of operations and what the operations do. We need a function that will translate our mathematical notations into theirs. Thankfully each of them provided templates for the notation they use to make it easier for us to translate. The way they structure their formulas is that they prioritize the execution of the formulas from right to left. For example: If an Andromedan has a formula like, 3 + 5 - 3 * 3 + 2, regardless of what the operations are, they will group it as, (3 + (5 - (3 * (3 + 2)))). You are given two main things, the first is a list that contains the formula in our mathematical notation, each element in the list represents an element in the mathematical notation. For example if we have 3 + 5 - 3* 3 + 2 this will be represented as [3, +, 5, 7, 3, '*', 3, '+', 2). Second resource you are given will be a templates dictionary that maps each of the operations to string templates. For example "+' might be mapped to the string template 'add({0}, {1})' where '{0}' and '{1}' are operands that should be replaced with the operands in the regular mathematical notation. So in this case ' {0}' should be replaced with 3 and '{1}' should be replaced with everything else, namely '(5 - (3 * (3 + 2))) but with its operations translated as well. See the examples in the doctests for more information. You need to return the translated equation in a string format. Restrictions 1. You should use recursion to solve the problem. 2. No loops or map operations are allowed. 3. Do input validation Note: For this question ONLY, you are allowed to use list comprehension inside all() function for input validation. You cannot use other types of loop to make assertions. def build_repr (operations, templates): Build the representation of the series of operations with the Given templates. The operations are prioritized from right to left. Restrictions: You should use recursion. You should do input validation. Parameters: operations (list(str)): List of numbers and operations to be executed on them Returns: (str): String representation of the operations >>> build_repr([0, 't', 1, '-', 3], {'+': '({0}, +, {1}}', '-': '({0}, -, {1})'}) '(0, +, (1, -, 3))' >>> build_repr([0, 't', 1, '-', 3], I {'+': '({0}, add, {1})', -':'({0}, minus, {1})'}) '(0, add, (1, minus, 3))' >>> build_repr([0, 't', 1, '-', 3], 1 {'+': '({0}, add, {1}}', 'L':'({0}, minus, {1})'}) '(0, add, (1, minus, 3))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
