Smeech
InterviewTime limit1sMemory limit128 MB
Parse a prefix Smeech expression with probabilistic plus or minus operators and compute its expected value to two decimals.
- Level
Easy3 of 10
- Topics
- Recursion, Math, Probability, Implementation
- Solved
- No attempts yet
Problem
Professor Octastichs has invented a new programming language called Smeech. A Smeech expression is one of the following:
- a positive or negative integer, or
- an expression of the form
(p e1 e2), wherepis a real number between 0 and 1 (inclusive) ande1,e2are Smeech expressions.
The value represented by a Smeech expression is defined as follows:
- An integer represents itself.
(p e1 e2)representsx + ywith probabilityp, andx - ywith probability1 - p, wherexis the value ofe1andyis the value ofe2.
Each add-or-subtract choice is made independently. Given a Smeech expression, compute its expected value.
Input
The input contains several Smeech expressions, one per line, followed by a line containing (), which marks the end of the input.
Output
For each expression, output its expected value rounded to two decimal places.