- 2

Write Python functions for the following operations:   addpoly(p1,p2)  multpoly(p1,p2) that add and multiply two polynomials, respectively.

Let us consider polynomials in a single variable x with integer coefficients: for instance, 3x^4 - 17x^2 - 3x + 5.  Each term of the polynomial can be represented as a pair of integers (coefficient,exponent).  The polynomial itself is then a list of such pairs.  We have the following constraints to guarantee that each polynomial has a unique representation: -- Terms are sorted in descending order of exponent -- No term has a zero cofficient -- No two terms have the same exponent -- Exponents are always nonnegative For example, the polynomial introduced earlier is represented as    [(3,4),(-17,2),(-3,1),(5,0)] The zero polynomial, 0, is represented as the empty list [], since it has no terms with nonzero coefficients. You may assume that the inputs to these functions follow the representation given above.  Correspondingly, the outputs from these functions should also obey the same constraints.

23rd Aug 2016, 3:31 PM
Gaurav Kumar Tiwari
Gaurav Kumar Tiwari - avatar
7 Antworten
+ 1
arnab dutta multpoly is giving wrong answer
13th Sep 2018, 2:40 PM
Anuj Gupta
Anuj Gupta - avatar
0
I'm not sure if this is what you wanted but effectively it adds and multiples 2 polynomials: import numpy as np def addpoly(p1, p2): poly1 = np.poly1d(p1) poly2 = np.poly1d(p2) result = poly1 + poly2 print(result) def multpoly(p1, p2): poly1 = np.poly1d(p1) poly2 = np.poly1d(p2) re = poly1 * poly2 print(re) equ1 = [3.0, 0.0, -17.0, -3.0, 5.0] equ2 = [4.0, 3.0, 5.0] #test functions addpoly(equ1, equ2) multpoly(equ1, equ2)
23rd Aug 2016, 6:18 PM
Jc Scheepers
0
def multpoly(p1, p2): for i in range(len(p1)-1): for item in p2: p1[i] = ((p1[i][0] * item[0]), (p1[i][1] + item[1])) p2.remove(item) return p1 right code for mult function.
5th Feb 2020, 3:33 PM
pulkeet gupta
pulkeet gupta - avatar
0
def listtodict(poly): dpoly = {} for term in poly: coeff = term[0] exp = term[1] dpoly[exp] = coeff return(dpoly) def dicttolist(dpoly): lpoly = [] for exp in sorted(dpoly.keys()): lpoly.append((dpoly[exp],exp)) lpoly.reverse() return(lpoly) # dpolyadd: initialize sum to dpoly1 and either update term or add a new term from dpoly2 def dpolyadd (dpoly1,dpoly2): sumpoly = {} for exp in dpoly1.keys(): sumpoly[exp] = dpoly1[exp] for exp in dpoly2.keys(): if exp in sumpoly.keys(): sumpoly[exp] = sumpoly[exp] + dpoly2[exp] else: sumpoly[exp] = dpoly2[exp] return(sumpoly) # dpolymult: compute each cross term and update result multpoly def dpolymult (dpoly1,dpoly2): multpoly = {} for exp1 in dpoly1.keys(): for exp2 in dpoly2.keys(): newexp = exp1 + exp2 newcoeff = dpoly1[exp1] * dpoly2[exp2] if newexp in multpoly.keys(): multpoly[newexp] = multpoly[newexp] + newcoeff else: multpoly[newexp] = newcoeff return(multpoly) # Remove 0 coefficient terms def cleanup(dpoly): dpolyclean = {} for exp in dpoly.keys(): if dpoly[exp] != 0: dpolyclean[exp] = dpoly[exp] return(dpolyclean) # Convert to dictionary, apply operations on dictionaries, convert back def addpoly(p1,p2): d1 = listtodict(p1) d2 = listtodict(p2) res = dpolyadd(d1,d2) return(dicttolist(cleanup(res))) def multpoly(p1,p2): d1 = listtodict(p1) d2 = listtodict(p2) res = dpolymult(d1,d2) return(dicttolist(cleanup(res)))
22nd Apr 2020, 7:01 AM
DebRaj Mondal
DebRaj Mondal - avatar
- 1
What is the value of pairs after the following assignment? pairs = [ (x,y) for x in range(4) for y in range(3) if (x+y)%3 == 0 ]
13th Sep 2018, 4:44 PM
arpita halder
arpita halder - avatar
- 2
def addpoly(p1, p2): for i in range(len(p1)): for item in p2: if p1[i][1] == item[1]: p1[i] = ((p1[i][0] + item[0]), p1[i][1]) p2.remove(item) p3 = p1 + p2 for item in (p3): if item[0] == 0: p3.remove(item) return sorted(p3) def multpoly(p1, p2): for i in range(len(p1)): for item in p2: p1[i] = ((p1[i][0] * item[0]), (p1[i][1] + item[1])) p2.remove(item) return p1
13th Sep 2018, 2:06 PM
Arnab Dutta
Arnab Dutta - avatar
- 11
Let us consider polynomials in a single variable x with integer coefficients: for instance, 3x4 - 17x2 - 3x + 5. Each term of the polynomial can be represented as a pair of integers (coefficient,exponent). The polynomial itself is then a list of such pairs. We have the following constraints to guarantee that each polynomial has a unique representation: Terms are sorted in descending order of exponent No term has a zero cofficient No two terms have the same exponent Exponents are always nonnegative For example, the polynomial introduced earlier is represented as [(3,4),(-17,2),(-3,1),(5,0)] The zero polynomial, 0, is represented as the empty list [], since it has no terms with nonzero coefficients. Write Python functions for the following operations: addpoly(p1,p2) multpoly(p1,p2) that add and multiply two polynomials, respectively. You may assume that the inputs to these functions follow the representation given above. Correspondingly, the outputs from these functions should also obey the same constraints. Hint: You are not restricted to writing just the two functions asked for. You can write auxiliary functions to "clean up" polynomials – e.g., remove zero coefficient terms, combine like terms, sort by exponent etc. Build a library of functions that can be combined to achieve the desired format. You may also want to convert the list representation to a dictionary representation and manipulate the dictionary representation, and then convert back. Some examples: >>> addpoly([(4,3),(3,0)],[(-4,3),(2,1)]) [(2, 1),(3, 0)] Explanation: (4x3 + 3) + (-4x3 + 2x) = 2x + 3 >>> addpoly([(2,1)],[(-2,1)]) [] Explanation: 2x + (-2x) = 0 >>> multpoly([(1,1),(-1,0)],[(1,2),(1,1),(1,0)]) [(1, 3),(-1, 0)] Explanation: (x - 1) * (x2 + x + 1) = x3 - 1
11th Sep 2018, 6:35 AM
prasad naidu
prasad naidu - avatar