Answer to Question #245465 in Python for toffer

Question #245465

Given polynomial, write a program that prints polynomial in Cix^Pi + Ci-1x^Pi-1 + .... + C1x +

The first line contains a single integer N.

Next N lines contain two integers Pi, Ci separated with space, where Pi denotes power and Ci denotes coefficient of Pi.Output


For term Cix^Pi, if the coefficient of the term Ci is 1, print x^Pi instead of 1x^Pi.Explanation

N <= 100

0 <= Pi < 1000

-1000 <= Ci <= 1000

Sample Input

4

0 5

1 0

2 10

3 6

Sample Output

6x^3 + 10x^2 + 5

Failed Testcases:

Your Output


7x^4 + 6x^3 + 1x^2 + 3 + 2

Expected


7x^4 + 6x^3 + x^2 + 3x + 2






1
Expert's answer
2021-10-02T01:36:55-0400
def tostring_polynom(p):
  res = ''
  first = True
  for Pi in sorted(p, reverse=True):
    Ci = p[Pi]
    if first:
      if Ci == 0 and Pi == 0:
        return '0'
       
      if Ci == 1 and Pi != 0:
        pass
      elif Ci == -1 and Pi != 0:
        res = '-'
      else:      
        res = f'{Ci}'
      first = False
       
    else:     
      if Ci > 0:
        res += ' + '
      elif Ci < 0:
        res += ' - '
      else:
        continue
      res += f'{abs(Ci)}'
     
    if Pi == 0:
      continue
    if Pi == 1:
      res += 'x'
    else:
      res += f'x^{Pi}'
  return res
def read_polynom():
  n = int(input())
  p = {}
  for i in range(n):
    L = input().split()
    Pi = int(L[0])
    Ci = int(L[1])
    p[Pi] = Ci
  return p

def add_polynomilas(p,q):
  final_dict = {}
  if len(p) >= len(q):
    for k,v in p.items():
      final_dict[k] = v + q.get(k,0)
    final_keys = list(final_dict.keys())
    for k1,v1 in q.items():
      if k1 not in final_keys:
        final_dict[k1] = v1
  else:
    for k,v in q.items():
      final_dict[k] = v + p.get(k,0)
    final_keys = list(final_dict.keys())
    for k1,v1 in p.items():
      if k1 not in final_keys:
        final_dict[k1] = v1
  return final_dict
     

def main():
  p = read_polynom()
  #q = read_polynom()
  #r = add_polynomilas(p, q)
  res = tostring_polynom(p)
  final_res = ""
  count = 0
  vals = res.split("+")
  for val in vals:
    if "x" in val:
      test_val = int(val.split("x")[0])
      if test_val==1:
        if count ==0:
          final_res=final_res+val.replace("1","")
        else:
          final_res=final_res+"+"+val.replace("1","")
      else:
        if count==0:
          final_res = final_res +str(val)
        else:
          final_res =final_res+ "+"+str(val)
      count+=1
    else:
      final_res =final_res+ "+"+str(val)
      count+=1
  print(final_res)
  #print(f'Add {tostring_polynom(p)} to {tostring_polynom(q)}')
  #print(f'results {tostring_polynom(r)}')
   

if __name__ == '__main__':
  main()

Sample Input Output

5

0 2

1 3

2 1

3 6

4 7

7x^4 + 6x^3 + x^2 + 3x + 2


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS