Question

I'm new to python and i'm trying to solve the gcd of polynomials task, but i got stuck at division of polynomials.

f=[1,2,3,4,5,6,7,8,9]
g=[7,8,9,10,11]

n=len(f)
m=len(g)

c=0
z=m


print f[0:n]
print g[0:m]

while not (z==n):
    divisor=f[0]/g[0]
    for i in range (0,m):
        rem=f[i]-g[i]*divisor
        print i
    f=rem*1
    z=z+1
    c=0
print rem[0:len(rem)]

I get error at line divisor=f[0]/g[0]. Here is the error:

TypeError: 'int' object has no attribute 'getitem' 
Was it helpful?

Solution

In your loop you are reassigning the list f to be an int by doing

f=rem*1

which is the cause of your error. Assuming your list f is f = [1,2,3] and rem is say, 3. This is what is happening

In [149]: f = [1,2,3]
In [150]: rem = 3
In [151]: f = rem * 1
In [152]: f
Out[152]: 3

So on the next iteration f[0] makes no sense, since f is no longer a list. Change

f = rem * 1

to

f[i] = rem * 1

Also, the last line in your code will throw an error, since you are trying to find length of an integer. rem in your code is not a list, but an integer, so it you can not do indexing on it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top