title: Add Two Polynomials Python Program
body: <p><strong>In this code there are four test cases. 1st and 2nd and 4th test cases are camed successfully but 3rd test case was not getting expected output. Please help me I got 12x^4 + 9x^3 - 5x^2x - 1 as output in 3rd test case but expected output is 12x^4 + 9x^3 - 5x^2 - x - 1</strong></p>
  <p><em>For Add two polynomials program question and 4 test cases Inputs and Outputs see below url</em></p>
  <p><a href="https://drive.google.com/file/d/1dEzy0tHPt-dveknUMw8ml85IARaoLcsE/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1dEzy0tHPt-dveknUMw8ml85IARaoLcsE/view?usp=sharing</a></p>
  <p><em>The above link contains Add two polynomials Question, 4 test cases and Inputs and Outputs</em></p>
  <pre><code>def readPolynomialFromUser():
      number_polynomials = int(input())
      P=[0 for i in range(number_polynomials)]
      for i in range(0,number_polynomials):
          index_p,index_c = input().split(' ')
          P[int(index_p)] = int(index_c)
      return P
  
  
  def computePolynomialSum(A,B):
      if(len(A)&gt;=len(B)):
          polynomials_sum=[0 for i in range(len(A))]
          for i in range(0,len(A)):
              if(i&gt;=len(B)):
                  B.append(0)
              polynomials_sum[i]=A[i]+B[i]
          return polynomials_sum
  
  
      return -1
  
  
  
  
  A=readPolynomialFromUser()
  B=readPolynomialFromUser()
  polynomials_sum=computePolynomialSum(A,B)
  if(polynomials_sum==-1):
      polynomials_sum=computePolynomialSum(B,A)
  
  if(sum(polynomials_sum)!=0):
      for i in range(len(polynomials_sum)-1,0,-1):
          if polynomials_sum[i] != 0:
              if i!=1:
                  if polynomials_sum[i]&gt;0:
                      if i==len(polynomials_sum)-1:
                          #if polynomials_sum[i]&lt;0:
                          #    polynomials_sum[i]=-1*polynomials_sum[i]
                          #    print (f' - {polynomials_sum[i]}x^{i}',end='')
                          #else: #redundent
                          if polynomials_sum[i]==1:
                              print (f'x^{i}',end='')
                          else:
                              print (f'{polynomials_sum[i]}x^{i}',end='')
                      else:
                          print (f' + {polynomials_sum[i]}x^{i}',end='')
                  else:
                      if polynomials_sum[i]!=-1:
                          #if polynomials_sum[i]&lt;0: #redundent
                          polynomials_sum[i]=-1*polynomials_sum[i]
                          if i==len(polynomials_sum)-1: #add p0 = i condition
                              print (f'-{polynomials_sum[i]}x^{i}',end='')
                          else:
                              print (f' - {polynomials_sum[i]}x^{i}',end='')
                      else:
                              if i==len(polynomials_sum)-1: #add p0 = i condition
                                  print (f'-x^{i}',end='')
                              else:
                                  print (f' - x^{i}',end='')
              else:
                  if polynomials_sum[i]&gt;0:
                      if i==len(polynomials_sum)-1: #add p0 = i condition
                          if polynomials_sum[i]==1:
                              print (f'x',end='')
                          else:
                              print (f'{polynomials_sum[i]}x',end='')
                      else:
                          if polynomials_sum[i]==1:
                              print (f' + x',end='')
                          else:
                              print (f' + {polynomials_sum[i]}x',end='')
                  else:
                      #if polynomials_sum[i]&lt;0: # redundent
                      polynomials_sum[i]=-1*polynomials_sum[i]
                      if i==len(polynomials_sum)-1: #add p0 = i condition
                          if polynomials_sum[i]==-1:
                              print (f'-x',end='')
                          else:
                              print (f'-{polynomials_sum[i]}x',end='')
                      else:
                          if polynomials_sum[i]==-1:
                              print (f' - x',end='')
                          else:
                              print (f' - {polynomials_sum[i]}x',end='')
  
      if polynomials_sum[0]&lt;0:
          polynomials_sum[0]=-1*polynomials_sum[0]
          if len(polynomials_sum)-1 == 0: #add p0 = i condition
              print (f'-{polynomials_sum[0]}',end='')
          else:
              print (f' - {polynomials_sum[0]}',end='')
      else:
          if len(polynomials_sum)-1 == 0: #add p0 = i condition
              print (f'{polynomials_sum[0]}',end='')
          else:
              print (f' + {polynomials_sum[0]}',end='')
  else:
      print(str(polynomials_sum[0]))
  </code></pre>