Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # a) Sa se defineasca o lista de vehicule autosortabila
- # b) Sa se numere cate vehicule se afla in lista
- # c) Sa se defineasca o functie care filtreaza si obtine o lista de automturisme pornind
- # # de la lista initiala
- # d) analog -> o lista de camioane
- # 1. Vehicul - nrInmatriculare / anInmatriculare
- # 2(1) Autoturism - motor / nrLocuri
- # 3(1) Camion - tonaj / nrRemorci
- class Vehicul():
- def __init__(self, nrInmatriculare, anInmatriculare):
- self.nrInmatriculare = nrInmatriculare
- self.anInmatriculare = anInmatriculare
- self.link = None
- def setareLink(self, element):
- self.link = element
- def getLink(self):
- return self.link
- def __gt__(self, element):
- if element != None:
- if element.anInmatriculare < self.anInmatriculare:
- return True
- else:
- return False
- def __repr__(self):
- return "Vehiculul " + str(self.nrInmatriculare) + " " + str(self.anInmatriculare)
- class Autoturism(Vehicul):
- def __init__(self, nrInmatriculare, anInmatriculare, motor, nrLocuri):
- super().__init__(nrInmatriculare, anInmatriculare)
- self.motor = motor
- self.nrLocuri = nrLocuri
- def __repr__(self):
- return "Autoturismul " + str(self.nrInmatriculare) + " " + str(self.anInmatriculare) + \
- " " + str(self.motor) + " " + str(self.nrLocuri)
- def clonare(self):
- return Autoturism(self.nrInmatriculare, self.anInmatriculare, self.motor, self.nrLocuri)
- class Camion(Vehicul):
- def __init__(self, nrImatriculare, anInmatriculare, tonaj, nrRemorci):
- super().__init__(nrImatriculare, anInmatriculare)
- self.tonaj = tonaj
- self.nrRemorci = nrRemorci
- def __repr__(self):
- return "Camionul " + str(self.nrInmatriculare) + " " + str(self.anInmatriculare) + \
- " " + str(self.tonaj) + " " + str(self.nrRemorci)
- def clonare(self):
- return Camion(self.nrInmatriculare, self.anInmatriculare, self.tonaj, self.nrRemorci)
- class Lista():
- def __init__(self):
- self.root = None
- def adauga(self, element):
- if element != None: #daca elementul nu este gol
- # in cazul in care adaugi primul element
- if self.root == None:
- self.root = element
- self.link = None
- # in cazul in care adaugi in mijlocul listei
- elif (element > self.root):
- succesor = None
- predecesor = None
- x = self.root # variabila locala
- while element > x.getLink() and x.getLink() != None:
- x = x.link
- if x.link == None: #adaugare element la capat
- x.link = element
- else: #adaugare in pozitie intermediara
- succesor = x.link
- predecesor = x
- element.link = succesor
- predecesor.link = element
- else:
- element.link = self.root
- self.root = element
- # in cazul in care adaugi la capatul listei
- # nr 1 2 3 4 5
- # root None None 1 2 3 4
- # link 1 2 3 4 5 6
- def numara(self):
- numarator = 0
- i = self.root
- while i!=None:
- numarator += 1
- i = i.link
- return numarator
- def filtreazaAutoturisme(self):
- listaAutoturisme = Lista()
- i = self.root
- #print("i este" + str(i))
- while i != None:
- if isinstance(i, Autoturism) == True:
- #i.clonare()
- listaAutoturisme.adauga(i.clonare())
- # print(i)
- i = i.link
- #print("i.link este " + str(i.link))
- return listaAutoturisme
- def filtreazaCamioane(self):
- listaCamioane = Lista()
- i = self.root
- while i != None:
- if isinstance(i, Camion) == True:
- listaCamioane.adauga(i.clonare())
- i = i.link
- return listaCamioane
- def __repr__(self):
- s = " "
- i = self.root
- while i!=None:
- s = s + str(i) + " / "
- #print(i)
- i = i.link
- return s
- autoturism1 = Autoturism("CJ01TRE", 2004, 100, 4)
- print(autoturism1)
- camion1 = Camion("B01BMW", 2002, 80, 2)
- print(camion1)
- camion2 = Camion("TM01BMW", 1998, 70, 1)
- print(camion2)
- camion3 = Camion("B01BMW", 2008, 60, 4)
- print(camion3)
- autoturism2 = Autoturism("BV01BMW", 2010, 10, 1)
- print(autoturism2)
- autoturism4 = Autoturism("TM01BMW", 2012, 10, 1)
- print(autoturism4)
- print("Este mai mare: ")
- if camion1 > autoturism1:
- print(camion1)
- print("-----------------")
- lista1 = Lista()
- lista1.adauga(autoturism1)
- lista1.adauga(camion1)
- lista1.adauga(camion2)
- lista1.adauga(camion3)
- lista1.adauga(autoturism2)
- lista1.adauga(autoturism4)
- print(lista1)
- print("In lista sunt " + str(lista1.numara()) + " elemente")
- print("Filtrare")
- print("Autoturisme:")
- listaAutoturisme = lista1.filtreazaAutoturisme()
- #print(isinstance(autoturism2, Autoturism))
- print(listaAutoturisme)
- print("Camioane:")
- listaCamioane = lista1.filtreazaCamioane()
- print(listaCamioane)
- #print(type(lista1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement