input: 
  def optimize_equation(equation: str) -> str:
      """
      Takes an equation consisting of variables and their counts, and optimizes it
      by reducing the repeated variables into a single term with an exponent.
  
      >>> optimize_equation('x + x + y + y + y')
      'x^2 + y^3'
      """
      variables = []
      for term in equation.split('+'):
          var, count = term.strip().split(' ')
          variables.append((var, int(count)))
  
      new_equation = ""
      for var, count in variables:
          new_equation += (var + '^' + str(count)) + ' + '
      return new_equation[:-3]
  
output:     
      variables = []
      for term in equation.split('+'):
          var = term.strip()
          if var in [v[0] for v in variables]:
              for i in range(len(variables)):
                  if variables[i][0] == var:
                      variables[i] = (var, variables[i][1] + 1)
          else:
              variables.append((var, 1))
  
      new_equation = ""
      for var, count in variables:
          new_equation += (var + '^' + str(count)) + ' + '
      return new_equation[:-3]
  