Chariots of fire P44592


Statement
 

pdf   zip

html

Telecommunication engineering is tough. Every year, students learn how to apply the rules of equivalent resistance:

  • Series setting: If you have two resistors connected one after the other, without any other connection in the middle, the equivalent resistance from the beginning of the first resistor to the end of the second resistor is just the sum of the two resistances.

  • Parallel setting: If you have two resistors connected in parallel, so that both beginnings are connected together and both ends are connected together, the equivalent resistance is the inverse of the sum of the inverses of the resistances. The extension of this rule also applies if more than two resistors are connected in parallel.

Your professor here at UPC went nuts and now wants you to apply this set of rules to a very complicated circuit of resistors. However, he swears that you can resolve it with just these two formulas. He is even going to tell you which resistors are connected in parallel and which in series! In the old times it wasn’t like that! Well …

Input

Input consists of several cases. Every case is a (perhaps huge) string describing a circuit. An ‘S’ begins a series setting, and a ‘P’ begins a parallel setting. Between parentheses there is a comma-separated list with two or more descriptions for the subcircuits. An ‘R’, together with a strictly positive real number between parentheses, describes each resistor. There are no spaces.

Output

For every circuit, its equivalent resistance with three digits after the decimal point. The cases have no precision issues.

Public test cases
  • Input

    R(2)
    S(R(2),R(5.5))
    S(R(3),R(2),R(1))
    S(S(R(3),R(2)),R(1.0000))
    P(R(2),R(3))
    P(R(1),R(2),R(3))
    P(S(R(2),R(1)),S(R(4),R(2)),R(2))
    

    Output

    2.000
    7.500
    6.000
    6.000
    1.200
    0.545
    1.000
    
  • Information
    Author
    Ricardo Martín
    Language
    English
    Official solutions
    C++
    User solutions
    C++