Math expressions can be represented as trees. We will rename the empty parent dataclass as Expression.

Then, we will have a BinaryExpression dataclass:

To actually make this work, we need one further twist: instead of an EMPTY instance, we’ll instead have an IntegerExpression dataclass with a single field:

The function evaluate_math is meant to process these complex Expression trees, and we have already written the logic that will separate out the base case from the recursive case. However, you will need to fill in the rest such that the code correctly evaluates the mathematical expressions.

Note: This problem is different from the other ways we’ve used recursion so far, and it’s meant to highlight how much variety you can see with these problems. It is still worth looking closely at!