title: Hidden test case failing in Google foobar Challenge
body: <p>I am attempting Doomsday Fuel in the Google Foobar challenge. Currently, I am passing every test case except one. There also have been similar questions asked about this challenge where the issue was not factoring in that S0 could be a potential terminating state. I have already accounted for that in my code. Could I please get some advice on what to do?</p>
  <p>Question:</p>
  <pre><code>Doomsday Fuel
  =============
  
  Making fuel for the LAMBCHOP's reactor core is a tricky process because of the exotic matter involved. It starts as raw ore, then during processing, begins randomly changing between forms, eventually reaching a stable form. There may be multiple stable forms that a sample could ultimately reach, not all of which are useful as fuel. 
  
  Commander Lambda has tasked you to help the scientists increase fuel creation efficiency by predicting the end state of a given ore sample. You have carefully studied the different structures that the ore can take and which transitions it undergoes. It appears that, while random, the probability of each structure transforming is fixed. That is, each time the ore is in 1 state, it has the same probabilities of entering the next state (which might be the same state).  You have recorded the observed transitions in a matrix. The others in the lab have hypothesized more exotic forms that the ore can become, but you haven't seen all of them.
  
  Write a function solution(m) that takes an array of array of nonnegative ints representing how many times that state has gone to the next state and return an array of ints for each terminal state giving the exact probabilities of each terminal state, represented as the numerator for each state, then the denominator for all of them at the end and in simplest form. The matrix is at most 10 by 10. It is guaranteed that no matter which state the ore is in, there is a path from that state to a terminal state. That is, the processing will always eventually end in a stable state. The ore starts in state 0. The denominator will fit within a signed 32-bit integer during the calculation, as long as the fraction is simplified regularly. 
  
  For example, consider the matrix m:
  [
    [0,1,0,0,0,1],  # s0, the initial state, goes to s1 and s5 with equal probability
    [4,0,0,3,2,0],  # s1 can become s0, s3, or s4, but with different probabilities
    [0,0,0,0,0,0],  # s2 is terminal, and unreachable (never observed in practice)
    [0,0,0,0,0,0],  # s3 is terminal
    [0,0,0,0,0,0],  # s4 is terminal
    [0,0,0,0,0,0],  # s5 is terminal
  ]
  So, we can consider different paths to terminal states, such as:
  s0 -&gt; s1 -&gt; s3
  s0 -&gt; s1 -&gt; s0 -&gt; s1 -&gt; s0 -&gt; s1 -&gt; s4
  s0 -&gt; s1 -&gt; s0 -&gt; s5
  Tracing the probabilities of each, we find that
  s2 has probability 0
  s3 has probability 3/14
  s4 has probability 1/7
  s5 has probability 9/14
  So, putting that together, and making a common denominator, gives an answer in the form of
  [s2.numerator, s3.numerator, s4.numerator, s5.numerator, denominator] which is
  [0, 3, 2, 9, 14].
  </code></pre>
  <p>My Solution (Python):</p>
  <pre><code>import fractions as f
  
  def identity_matrix(d):
      result=list()
      for i in range(d):
          temp=list()
          for j in range(d):
              if j==i:
                  temp.append(1)
              else:
                  temp.append(0)
          result.append(temp)
      return result
  
  def subtract_matrix(M1, M2):
      
      #Error Check
      if len(M1)!=len(M2) or len(M1[0])!=len(M2[0]):
          print('subtract_matrix DIMENSION ERROR')
          return None
      
      #Function Code
      result=list()
      for i in range(len(M1)):
          temp=list()
          for j in range(len(M2)):
              temp.append(M1[i][j]-M2[i][j])
          result.append(temp)
      return result
  
  
  def multiply_matrix(M1, M2):
  
      #Error Check
      if len(M1[0])!=len(M2):
          print('multiply_matrix DIMENSION ERROR')
          return None
  
      #Function Code
      result=list()
      for i in range(len(M1)):
          temp=list()
          for j in range(len(M2[0])):
              element=0
              for k in range(len(M1[0])):
                  element+=M1[i][k]*M2[k][j]
              temp.append(element)
          result.append(temp)
      
      return result
  
  def normalize_matrix(m):
      for row_index in range(len(m)):
          if sum(m[row_index])==0:
              denom=1
          else:
              denom=sum(m[row_index])
          for p_index in range(len(m[row_index])):
              m[row_index][p_index]=f.Fraction(numerator=m[row_index][p_index], denominator=denom)
      return m
  
  def find_gcd(x,y):
      result=1
      for i in range(1,x+1):
          if y%i==0 and i&gt;result:
              result=i
      return result
  
  def lcm(x):
      result=x[0]
      for i in x:
          result*=i//find_gcd(result,i)
      return result
      
  
  def invert(matrix):
      n = len(matrix)
      inverse = [[f.Fraction(0) for col in range(n)] for row in range(n)]
      for i in range(n):
          inverse[i][i] = f.Fraction(1)
      for i in range(n):
          for j in range(n):
              if i != j:
                  if matrix[i][i] == 0:
                      return False
                  ratio = matrix[j][i] / matrix[i][i]
                  for k in range(n):
                      inverse[j][k] = inverse[j][k] - ratio * inverse[i][k]
                      matrix[j][k] = matrix[j][k] - ratio * matrix[i][k]
      for i in range(n):
          a = matrix[i][i]
          if a == 0:
              return False
          for j in range(n):
              inverse[i][j] = inverse[i][j] / a
      return inverse
  
  def solution(m):
      #Your code here
      n=len(m)
      
      if sum(m[0])==0:
          return [1]+[0]*(n-1)+[1]
  
      terminal_states=list()
      transition_states=list()
  
      for s in range(n):
          all_zero=True
          for i in range(n):
              if m[s][i]!=0:
                  all_zero=False
          if all_zero:
              m[s][s]=1
              terminal_states.append(s)
          if not all_zero:
              transition_states.append(s)
  
      
      if len(terminal_states)==1:
          end=[0]*(n)
          end[terminal_states[0]-1]=1
          end[-1]=1
          return end
  
      m=normalize_matrix(m)
  
      standard_form_m=list()
  
      for i in range(n+1):
          standard_form_m.append([0]*(n+1))
  
      for s_index in range(len(terminal_states)):
          standard_form_m[0][s_index+1]=terminal_states[s_index]
          standard_form_m[s_index+1][0]=terminal_states[s_index]
  
      for s_index in range(len(transition_states)):
          standard_form_m[0][s_index+len(terminal_states)+1]=transition_states[s_index]
          standard_form_m[s_index+len(terminal_states)+1][0]=transition_states[s_index]
  
      for i in range(1,n+1):
          for j in range(1,n+1):
              i=int(i)
              j=int(j)
              standard_form_m[i][j]=m[standard_form_m[i][0]][standard_form_m[0][j]]
  
      R=list()
      Q=list()
  
      for i in range(len(transition_states)):
          R.append([0]*len(terminal_states))
  
      for i in range(len(transition_states)):
          Q.append([0]*len(transition_states))
  
      for i in range(len(transition_states)):
          for j in range(len(terminal_states)):
              R[i][j]=standard_form_m[i+len(terminal_states)+1][j+1]/sum(standard_form_m[i+len(terminal_states)+1][1:])
              
      for i in range(len(transition_states)):
          for j in range(len(transition_states)):
              Q[i][j]=standard_form_m[i+len(terminal_states)+1][j+len(terminal_states)+1]/sum(standard_form_m[i+len(terminal_states)+1][1:])
  
      F=invert(subtract_matrix(identity_matrix(len(Q)),Q))
      FR=multiply_matrix(F,R)
      
      denominators=list()
      numerators=list()
  
      for p in FR[0]:
          denominators.append(p.denominator)
          numerators.append(p.numerator)
  
      LCM=lcm(denominators)
  
      for i in range(len(denominators)):
          if denominators[i]!=LCM:
              numerators[i]*=LCM//denominators[i]
  
      numerators.append(LCM)
  
      return numerators
  
  </code></pre>