title: Calculate the total resistance of a circuit given in a string
body: <p>I have really been struggling to solve this problem. This is the problem: </p>
  
  <blockquote>
    <p>Given a string describing the circuit, calculate the total resistance
    of the circuit.</p>
  </blockquote>
  
  <p>Here is an example:</p>
  
  <ul>
  <li>input: <code>3 5 S</code></li>
  <li>expected output: <code>8</code></li>
  </ul>
  
  <p>The operands in the string are trailed by the operator, denoting if the resistors are either in Series or Parallel. However let's analyze a more complicated circuit:</p>
  
  <ul>
  <li>input: <code>3 5 S 0 P 3 2 S P</code></li>
  <li>expected output: <code>0</code></li>
  </ul>
  
  <p>Step by step:</p>
  
  <ol>
  <li>The <code>3 5 S</code> at the beginning of the input gives us 8 and hence the first intermediate step is the string <code>8 0 P 3 2 S P</code>.</li>
  <li><code>8 0 P</code> gives us <code>0</code>, as one resistor is short-circuited and consequently we get <code>0 3 2 S P</code>.</li>
  <li><code>3 2 P</code> is <code>5</code>.</li>
  <li>and finally  <code>0 5 P</code> is <code>0</code>.</li>
  </ol>
  
  <p>Here is my attempt. I tried using recursion as it seemed like a problem that can be solved that way. Firstly I wrote a helper function:</p>
  
  <pre><code>def new_resistance(a,b,c):
  if c == '':
      if int(a) == 0 or int(b) == 0:
          return 0
      else:
          return 1/(1/int(a) + 1/int(b))
  else:
      return int(a) + int(b)
  </code></pre>
  
  <p>And the function that calculates the newn resistance of the circuit:</p>
  
  <pre><code>def resistance(array):
  if isinstance(array, int):
      return array
  else:
      if isinstance(array,list):
          temp = array
      else:
          temp = array.split(" ")
      i = 0
      while True:
          try:
              a = new_resistance(temp[i], temp[i+1], temp[i+2])
          except Exception as e:
              i += 1            
          if len(temp[i+3:]) == 0:
              return resistance(new_resistance(temp[i], temp[i+1], temp[i+2]))
          else:
              return resistance(temp[:i] + [new_resistance(temp[i], temp[i+1], temp[i+2])] + temp[i+3:])
  </code></pre>
  
  <p>The idea behind the program is to start at the beginning of the list and calculate the resistance of the first three elements of the list, then to append them at the beginning of a new list (without the three elements) and call the function again with the new list. Do this until only a single integer remains and return the integers. </p>
  
  <p>Any help is appreciated. </p>
  
  <hr>
  
  <p>UPDATE:</p>
  
  <p>The solution to the problem, using a stack and a parser similar to a NPR parser.</p>
  
  <pre><code>operator_list = set('PS')
  
  def resistance(circuit):
    temp = circuit.split(" ")
    stack = []
    for char in temp:
        if char in operator_list:
            a = new_resistance(stack.pop(), stack.pop(), char)
            print(a)
            stack.append(a)
        else:
            print(char)
            stack.append(char)
    return stack[-1]
  
  def new_resistance(a,b,c):
    if c == 'P':
        if float(a) == 0 or float(b) == 0:
            return 0
        else:
            return 1/(1/float(a) + 1/float(b))
    else:
        return float(a) + float(b)
  
  circuit = '3 5 S 0 P 3 2 S P'
  resistance(circuit)
  
  # 3
  # 5
  # 8.0
  # 0
  # 0
  # 3
  # 2
  # 5.0
  # 0
  </code></pre>
  
  <hr>