Python Forum

Full Version: I don't understand where is the error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import math
import random
def simili(a,b):
    r=True
    if(len(a.l)!=len(b.l)):
        r=False
    else:
        g=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
        for i in range(len(a.l)):
            for j in range(len(b.l)):
                if(a.l[i]==b.l[j] and a.p[i]==b.p[j]): g[i]=1
        tot=0
        for i in g: tot+=i
        if(tot!=len(a.l)): r=False
    return r
class polinomio():
    def __init__(self,p):
        self.p=p
    def sc(self):
        r=self.p[0].pri()
        for i in range(1,len(self.p)): r+=self.p[i].den()
        return r
class poly:
    def __init__(self,l,n,p):
        self.n=n
        self.p=p
        self.l=l
    def pri(self):
        r=""
        h=str(self.n)
        if(self.n==1): h=""
        if(self.n==-1): h="-"
        r=h
        for i in range(len(self.l)):
            if(self.p[i]==0): r+=""
            else:
                if(self.p[i]==1): r+=self.l[i]
                else: r+=self.l[i]+"<sup>"+str(self.p[i])+"</sup>"
        if(self.n==0): r=""
        return r
    def den(self):
        r=""
        h=str(self.n)
        if(self.n>0): h="+"+str(self.n)
        if(self.n==1): h="+"
        if(self.n==-1): h="-"
        r=h
        for i in range(len(self.l)):
            if(self.p[i]==0): r+=""
            else:
                if(self.p[i]==1): r+=self.l[i]
                else: r+=self.l[i]+"<sup>"+str(self.p[i])+"</sup>"
        if(self.n==0): r=""
        return r
    def sim(self,n):
        return poly(self.l,n,self.p)
def men(mon):
    return mono(mon.l,-mon.n,mon.p)
def bino(a,b):
    return "("+a.pri()+b.den()+")"
def trino(a,b,c):
    return "("+a.pri()+b.den()+c.den()+")"
def po():
    return random.randint(2,6)
def nu():
    n=(random.randint(1,9))
    if(random.randint(0,9999)<5000): n=-n
    return n
def let():
    l=["a","b","c","d","e","f","g","h","m","n","p","q","r","t","u","v","z"]
    return l[random.randint(0,len(l))]
def pp(n):
    return "<sup>"+str(n)+"</sup>"
def semplifica(ss):
    a=ss.p
    j=0
    nn=[]
    m=[]
    for i in range(20):
        nn+=[True]
    for i in range(len(a)-1):
        j+=1
        for h in range (j,len(a)-1):
            if(simili(a[i],a[h])==True):
                a[i].n=a[i].n+a[h].n
                a[h]=[]
                nn[h]=False
    print len(a)
    for i in range(len(a)):
        if(nn[i]==True): m+=[a[i]]
    print len(m)
    return polinomio(m)

verifica=open("test.html","w")
c=polinomio([poly(["s"],2,[2]),poly(["s"],2,[2]),poly(["d","m"],3,[1,-1])])
e=polinomio([poly(["s"],2,[2]),poly(["s"],2,[2]),poly(["d","m"],3,[1,-1])])
d=semplifica(e)
verifica.write(e.sc())
verifica.close()
Hallo everyone
I'm trying to make a polynomial class and it work, but i tried to make a simplifying the polynomial whit a function and the class stop to work correctly, and i don't understand why....
It's so frustrating
Anyone can help me? Thank you very much...
I cleaned up your code a bit. Remember, spaces are good and parentheses aren't needed in if statements. Also, please use more descriptive variable and function names.

It seems that the issue arises from semplifica. The semplifica function sets "a" to poly.p. Subsequently, the function changes poly.p every time that "a" is changed in the function. By the end, this sets poly.p to an empty list and that list does not have the den() method.

There are a couple comments in the code now too.

import math
import random

def simili(a, b):
    r = len(a.l) == len(b.l)

    if r:
        g = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
        for i in range(len(a.l)):
            for j in range(len(b.l)):
                if a.l[i] == b.l[j] and a.p[i] == b.p[j]:
                    g[i] = 1

        tot = 0
        for i in g: # g is constant
            tot += i

        r = tot == len(a.l)

    return r

class polinomio():

    def __init__(self, p):
        self.p = p

    def sc(self):
        r = self.p[0].pri()

        for i in range(1,len(self.p)):
            r += self.p[i].den()

        return r

class poly:

    def __init__(self, l, n, p):
        self.n = n
        self.p = p
        self.l = l # Never use "l" as a variable, it looks too much like "1" or "I"

    def pri(self):
        r = ""

        if self.n == 1:
            h = ""
        elif self.n == -1:
            h = "-"
        else: h = str(self.n)

        r = h

        for i in range(len(self.l)):
            if self.p[i] == 0:
                r += ""
            else:
                if self.p[i] == 1:
                    r += self.l[i]
                else: r += self.l[i] + "<sup>" + str(self.p[i]) + "</sup>"

        if self.n == 0:
            r = ""

        return r

    def den(self):
        r = ""

        if self.n > 0:
            h = "+"+str(self.n)
        elif self.n == 1:
            h = "+"
        elif self.n == -1:
            h = "-"
        else: h = str(self.n)

        r = h

        for i in range(len(self.l)):
            if self.p[i] == 0:
                r += ""
            else:
                if self.p[i] == 1:
                    r += self.l[i]
                else: r += self.l[i] + "<sup>" + str(self.p[i]) + "</sup>"

        if self.n == 0:
            r = ""

        return r

    def sim(self, n):
        return poly(self.l, n, self.p)

def men(mon):
    return mono(mon.l, -mon.n, mon.p)

def bino(a,b):
    return "(" + a.pri() + b.den() + ")"

def trino(a,b,c):
    return "(" + a.pri() + b.den() + c.den() + ")"

def po():
    return random.randint(2,6)

def nu():
    n = random.randint(1, 9)
    if random.randint(0, 9999) < 5000:
        n = 0

    return n

def let():
    x = ["a","b","c","d","e","f","g","h","m","n","p","q","r","t","u","v","z"]
    return random.choice(x)

def pp(n):
    return "<sup>" + str(n) + "</sup>"

def semplifica(ss):
    a = ss.p
    j = 0
    nn = []
    m = []

    for i in range(20):
        nn += [True]

    for i in range(len(a)-1):
        j += 1
        for h in range(j,len(a)-1):
            if simili(a[i], a[h]):
                a[i].n = a[i].n + a[h].n
                a[h] = []
                nn[h] = False

    print(len(a))

    for i in range(len(a)):
        if nn[i]:
            m.append([a[i]])
    print(len(m))

    return polinomio(m)

verifica = open("test.html","w")
c = polinomio(
    [
        poly(["s"],2,[2]),
        poly(["s"],2,[2]),
        poly(["d","m"],3,[1,-1])
    ]
)
e = polinomio(
    [
        poly(["s"],2,[2]),
        poly(["s"],2,[2]),
        poly(["d","m"],3,[1,-1])
    ]
)
print(e.p)
d = semplifica(e)
verifica.write(e.sc())
verifica.close()
Thank you i solved!
One observation:

- on line 8 there is list of zeros. I counted that there are 26 zeros (maybe I miscounted :-). This is very error-prone way to create such a list. One can just:

>>> [0 for i in range(26)]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]