TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

Resistors

Problem statement, definition, constraints, and public examples.

Problem Statement

A simple network of resistors can be defined recursively as:

  • simple network ::= a simple network in parallel with another simple network
  • simple network ::= a simple network in series with another simple network
  • simple network ::= a single resistor

If two networks are in parallel with each other and have resistances r1 and r2, then the total resistance is 1/(1/r1+1/r2).
If two networks are in series with each other and have resistances r1 and r2, then the total resistance is r1+r2.
If a network is a single resistor, then its resistance is defined as the resistance of that single resistor.

You will be given a simple network of resistors represented as a String[], resistors. Each element of resistors will be either "P", "S", or a decimal number. If an element is a number, then it represents a single resistor with resistance defined by that number. If an element is "P", it defines a simple network whose two component simple networks consist of the next two simple networks in resistors. Similarly, if an element is "S", then the next two simple networks in resistors are in series. Thus, the total number of elements that are numbers will be equal to the total number of elements that are "S" or "P" plus 1. For example, {"S","5.3","P","40","60"} represents a simple network where a single resistor with resistance 5.3 is in series with two resistors in parallel with resistances 40 and 60:

            +----- 40 ----+
            |             |
---- 5.3 ---+             +----
            |             |
            +----- 60 ----+

Your task is to compute the total resistance of the network, and return the result as a double. Small rounding errors will be ignored when examining your result for correctness. As long as your result has a relative or absolute error of less than 1e-9, it will be judged correct.

Definition

Class:
Resistors
Method:
getResistance
Parameters:
String[]
Returns:
double
Method signature:
double getResistance(String[] resistors)
(be sure your method is public)

Constraints

  • Each element of resistors will be either "P", "S", or a decimal number.
  • resistors will contain between 1 and 50 elements, inclusive.
  • Each decimal number in resistors will be between 1 and 1000000, inclusive, and will be represented by between 1 and 50 characters, inclusive.
  • resistors will represent a valid network, such that for every "P" or "S" both of the subnetworks exist and there are no extraneous elements.
  • Each decimal number in resistors will be formatted as a sequence of digits ('0'-'9') with an optional decimal point, which may not be the first or last character of the number.

Examples

  1. {"S","5.3","P","40","60"}
    Returns: 29.3
    This is the network above. The resistance of the two resistors in parallel is 1/(1/60 + 1/40) = 24. This resistance, in series with 5.3 gives 29.3 total. Note that your solution need not return exactly 29.3.
  2. {"S","P","P","1.65","7.59","S","75","1.2","10"}
    Returns: 11.331670926296885
    This represents the following network: +--- 1.65 ---+ | | +----+ +--------+ | | | | | +--- 7.59 ---+ | | | ----+ +---- 10 ---- | | | | | | | | +-------- 75 ----- 1.2 ----+ The total resistance is 10 + 1 / (1 / (75 + 1.2) + 1 / 1.65 + 1 / 7.59).
  3. {"032.350"}
    Returns: 32.35
Back to all problems