-1

I am very new in writing Python code. I have to represent polynomial like this,

4x^230+7x^96+1

testP = [[4,512],[7,256],[1,0]]

def printPoly2(P):
    for i in range(len(P)):
        if P[i] != 0:
            print("%s x^%s + "%(P[i], i)),
            print

printPoly2(testP)

but my answer is not correct. I need help on this.

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
tani
  • 17
  • 8
  • 1
    I'm not making the connection between your opening example polynomial, and your representation of it as a list of lists. Should testP not be: [[4,230],[7,96],[1,0]] ? – Thomas Kimber Feb 02 '16 at 17:02
  • I guess it should. But P in your method is a list of lists. So P[i] returns a list and you compare it to != 0 which makes no sense. – Rainer Feb 02 '16 at 17:05
  • Unpacking the list of lists as follows should yield the results I think the OP is looking for 2 changes, first the *if* clause could be: if len(P[i]) != 0 And secondly, the following print statement as print("%s x^%s + "%(P[i][0], P[i][1])), – Thomas Kimber Feb 02 '16 at 17:08
  • Sorry, testP should be like this, testP = [[4,512],[7,256],[1,0]] – tani Feb 02 '16 at 17:14
  • Please mark my answer as solving (if so). ty – Rainer Feb 02 '16 at 17:40

4 Answers4

0

Your problem is in the print statement. By writing:

print("%s x^%s + "%(P[i], i))

You are actually printing the entire tuple. For example, P[0] equals the entire list [4, 512]. So in directly printing P[i] you're just printing the entire sublist.

Instead, you want to print each element in the sublist.

print("%s x^%s" % (P[i][0], P[i][1]))


Additionally, your solution as-is will print each part of the answer on a separate line. To fix this you have a couple options, but here's one that doesn't depend on the python version you have. Basically, you create a string that represents your result, and keep building on it as you go through the loop. Then at the very end of the function, you print that string.

result = '' # initialize an empty string before your loop

Then inside your loop replace the print call with:

result = "%s + %s x^%s" % (result, P[i][0], P[i][1]

And at the very end of the function you can print the string.

print (result)
xgord
  • 4,606
  • 6
  • 30
  • 51
0

I (hopefully) improved your code with some pythonic magic. Now it´s a list of tuples and uses automatic tuple-unwrapping.

testP = [(4, 230), (7, 96), (1, 0)]

def printPoly2(P):
    for i, e in P:
        if e != 0:
            print("{} x^{} + ".format(i, e))

printPoly2(testP)

Prints out

4 x^230 + 
7 x^96 + 
Rainer
  • 761
  • 5
  • 20
0

Just had 5 more minutes... here you go

testP = [(4, 230), (7, 96), (1, 0)]

def printPoly2(polynom):
    parts = []
    for i, e in polynom:
        parts.append(_format_polynome(i,e))

    print " + ".join(parts)

def _format_polynome(i, e):
    if e == 0:
        return "{}".format(i)
    else:
        return "{} x^{}".format(i, e)

printPoly2(testP)
Rainer
  • 761
  • 5
  • 20
0

this is how I would write this code in something I think is a more pythonic way of writting it:

def poly2(P):
    result = []
    for item in P:
        n, e = item
        if e == 0:
            result.append("1")
        else:
            result.append("{}x^{}".format(n, e))
    return " + ".join(result)

# 4x^230 + 7x^96 + 1
testP = [(4,230), (7,96), (1,0)]
print poly2(testP)

note that its more flexible to return a string representation than to print it directly from the function.

Lynch
  • 9,174
  • 2
  • 23
  • 34