Python Forum
I don't understand where is the error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I don't understand where is the error
#1
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...
Reply
#2
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()
Reply
#3
Thank you i solved!
Reply
#4
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]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error I don't understand finndude 2 5,012 Oct-12-2022, 02:43 PM
Last Post: finndude
  Don't understand error message Milfredo 2 1,995 Aug-24-2020, 05:00 PM
Last Post: Milfredo
Exclamation Get syntax error. Dont understand why beLIEve 21 14,459 Aug-25-2019, 02:28 PM
Last Post: ThomasL
  Syntax error for a reason I don't understand DanielCook 2 2,408 Aug-07-2019, 11:49 AM
Last Post: buran
  Error in script don't understand, please help! DanielR 0 2,026 Mar-15-2019, 12:39 PM
Last Post: DanielR
  I don't understand the error MasterGame 1 2,519 Jul-18-2018, 07:55 PM
Last Post: gontajones
  Index Error, can't understand why. Diamond 5 4,176 Jan-03-2018, 11:45 AM
Last Post: Diamond

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020