Answer to Question #224309 in Python for BHAGAVAN

Question #224309

If M = 4 and for polynomial A

For power 0, co-efficient is 5

For power 1, co-efficient is 0

For power 2, co-efficient is 10

For power 3, co-efficient is 6.


If N = 3 and for polynomial B

For power 0, co-efficient is 1

For power 1, co-efficient is 2

For power 2, co-efficient is 4.

Then polynomial A represents "6x^3 + 10x^2 + 5", the polynomial B represents "4x^2 + 2x + 1" and the addition of A and B is "6x^3 + 14x^2 + 2x + 6"


1
Expert's answer
2021-08-09T02:34:29-0400
def res(a, b):
    result = [0] * (max_power + 1)
    for i in range(max_power + 1):
        coefficient = a.get(i, 0) + b.get(i, 0)
        result[i] = (i, coefficient)
    result.reverse()
    return result


m = int(input())
p1 = {}
max_power = 0
for i in range(m):
    power, coefficient = map(int, input().split())
    max_power = max(max_power, power)
    p1[power] = coefficient
n = int(input())
p2 = {}
for i in range(n):
    power, coefficient = map(int, input().split())
    p2[power] = coefficient
    max_power = max(max_power, power)
final_list = res(p1, p2)
polynomial = ''
for i in range(len(final_list)):
    if final_list[i][1] == 0:
        continue
    if i == len(final_list) - 1:
        polynomial += str(final_list[i][1])
        if final_list[i][0]:
            polynomial += 'x'
            if final_list[i][0] > 1:
                polynomial += '^' + str(final_list[i][0])
    else:
        polynomial += str(final_list[i][1] if final_list[i][1] != -1 else '-') + 'x'
        if final_list[i][0] > 1:
            polynomial += '^' + str(final_list[i][0])
        polynomial += ' + '
if polynomial:
    print('Polynomial: ' + polynomial)
else:
    print('Polynomial: 0')
def res(a, b):
    result = [0] * (max_power + 1)
    for i in range(max_power + 1):
        coefficient = a.get(i, 0) + b.get(i, 0)
        result[i] = (i, coefficient)
    result.reverse()
    return result


m = int(input())
p1 = {}
max_power = 0
for i in range(m):
    power, coefficient = map(int, input().split())
    max_power = max(max_power, power)
    p1[power] = coefficient
n = int(input())
p2 = {}
for i in range(n):
    power, coefficient = map(int, input().split())
    p2[power] = coefficient
    max_power = max(max_power, power)
final_list = res(p1, p2)
polynomial = ''
for i in range(len(final_list)):
    if final_list[i][1] == 0:
        continue
    if i == len(final_list) - 1:
        polynomial += str(final_list[i][1])
        if final_list[i][0]:
            polynomial += 'x'
            if final_list[i][0] > 1:
                polynomial += '^' + str(final_list[i][0])
    else:
        polynomial += str(final_list[i][1] if final_list[i][1] != -1 else '-') + 'x'
        if final_list[i][0] > 1:
            polynomial += '^' + str(final_list[i][0])
        polynomial += ' + '
if polynomial:
    print('Polynomial: ' + polynomial)
else:
    print('Polynomial: 0')

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