Advertisement
Poganu

Untitled

Oct 9th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.30 KB | None | 0 0
  1. # a) Sa se defineasca o lista de vehicule autosortabila
  2. # b) Sa se numere cate vehicule se afla in lista
  3. # c) Sa se defineasca o functie care filtreaza si obtine o lista de automturisme pornind
  4. # #  de la lista initiala
  5. # d) analog -> o lista de camioane
  6.  
  7. # 1. Vehicul  - nrInmatriculare / anInmatriculare
  8. # 2(1) Autoturism - motor / nrLocuri
  9. # 3(1) Camion - tonaj / nrRemorci
  10.  
  11. class Vehicul():
  12.     def __init__(self, nrInmatriculare, anInmatriculare):
  13.         self.nrInmatriculare = nrInmatriculare
  14.         self.anInmatriculare = anInmatriculare
  15.         self.link = None
  16.  
  17.     def setareLink(self, element):
  18.         self.link = element
  19.  
  20.     def getLink(self):
  21.         return self.link
  22.  
  23.     def __gt__(self, element):
  24.         if element != None:
  25.             if element.anInmatriculare < self.anInmatriculare:
  26.                 return True
  27.             else:
  28.                 return False
  29.  
  30.     def __repr__(self):
  31.         return "Vehiculul " + str(self.nrInmatriculare) + " " + str(self.anInmatriculare)
  32.  
  33. class Autoturism(Vehicul):
  34.     def __init__(self, nrInmatriculare, anInmatriculare, motor, nrLocuri):
  35.         super().__init__(nrInmatriculare, anInmatriculare)
  36.         self.motor = motor
  37.         self.nrLocuri = nrLocuri
  38.  
  39.     def __repr__(self):
  40.         return "Autoturismul " + str(self.nrInmatriculare) + " " + str(self.anInmatriculare) + \
  41.             " " + str(self.motor) + " " + str(self.nrLocuri)
  42.  
  43.     def clonare(self):
  44.         return Autoturism(self.nrInmatriculare, self.anInmatriculare, self.motor, self.nrLocuri)
  45.  
  46. class Camion(Vehicul):
  47.     def __init__(self, nrImatriculare, anInmatriculare, tonaj, nrRemorci):
  48.         super().__init__(nrImatriculare, anInmatriculare)
  49.         self.tonaj = tonaj
  50.         self.nrRemorci = nrRemorci
  51.  
  52.     def __repr__(self):
  53.         return "Camionul " + str(self.nrInmatriculare) + " " + str(self.anInmatriculare) + \
  54.             " " + str(self.tonaj) + " " + str(self.nrRemorci)
  55.  
  56.     def clonare(self):
  57.         return Camion(self.nrInmatriculare, self.anInmatriculare, self.tonaj, self.nrRemorci)
  58.  
  59. class Lista():
  60.     def __init__(self):
  61.         self.root = None
  62.  
  63.     def adauga(self, element):
  64.         if element != None: #daca elementul nu este gol
  65.             # in cazul in care adaugi primul element
  66.             if self.root == None:
  67.                 self.root = element
  68.                 self.link = None
  69.             # in cazul in care adaugi in mijlocul listei
  70.             elif (element > self.root):
  71.                 succesor = None
  72.                 predecesor = None
  73.                 x = self.root # variabila locala
  74.                 while element > x.getLink() and x.getLink() != None:
  75.                     x = x.link
  76.                 if x.link == None: #adaugare element la capat
  77.                     x.link = element
  78.                 else: #adaugare in pozitie intermediara
  79.                     succesor = x.link
  80.                     predecesor = x
  81.                     element.link = succesor
  82.                     predecesor.link = element
  83.             else:
  84.                 element.link = self.root
  85.                 self.root = element
  86.             # in cazul in care adaugi la capatul listei
  87.  
  88. # nr           1      2    3    4    5
  89. # root  None  None    1    2    3    4
  90. # link  1      2      3    4    5    6
  91.     def numara(self):
  92.         numarator = 0
  93.         i = self.root
  94.         while i!=None:
  95.             numarator += 1
  96.             i = i.link
  97.         return numarator
  98.  
  99.     def filtreazaAutoturisme(self):
  100.         listaAutoturisme = Lista()
  101.         i = self.root
  102.         #print("i este" + str(i))
  103.         while i != None:
  104.             if isinstance(i, Autoturism) == True:
  105.                 #i.clonare()
  106.                 listaAutoturisme.adauga(i.clonare())
  107.             #    print(i)
  108.             i = i.link
  109.             #print("i.link este " + str(i.link))
  110.         return listaAutoturisme
  111.  
  112.     def filtreazaCamioane(self):
  113.         listaCamioane = Lista()
  114.         i = self.root
  115.         while i != None:
  116.             if isinstance(i, Camion) == True:
  117.                 listaCamioane.adauga(i.clonare())
  118.             i = i.link
  119.         return listaCamioane
  120.  
  121.     def __repr__(self):
  122.         s = " "
  123.         i = self.root
  124.         while i!=None:
  125.             s = s + str(i) + " / "
  126.             #print(i)
  127.             i = i.link
  128.         return s
  129.  
  130.  
  131.  
  132. autoturism1 = Autoturism("CJ01TRE", 2004, 100, 4)
  133. print(autoturism1)
  134.  
  135. camion1 = Camion("B01BMW", 2002, 80, 2)
  136. print(camion1)
  137.  
  138. camion2 = Camion("TM01BMW", 1998, 70, 1)
  139. print(camion2)
  140.  
  141. camion3 = Camion("B01BMW", 2008, 60, 4)
  142. print(camion3)
  143.  
  144. autoturism2 = Autoturism("BV01BMW", 2010, 10, 1)
  145. print(autoturism2)
  146.  
  147. autoturism4 = Autoturism("TM01BMW", 2012, 10, 1)
  148. print(autoturism4)
  149.  
  150. print("Este mai mare: ")
  151. if camion1 > autoturism1:
  152.     print(camion1)
  153.  
  154. print("-----------------")
  155. lista1 = Lista()
  156. lista1.adauga(autoturism1)
  157. lista1.adauga(camion1)
  158. lista1.adauga(camion2)
  159. lista1.adauga(camion3)
  160. lista1.adauga(autoturism2)
  161. lista1.adauga(autoturism4)
  162.  
  163. print(lista1)
  164. print("In lista sunt " + str(lista1.numara()) + " elemente")
  165.  
  166. print("Filtrare")
  167. print("Autoturisme:")
  168. listaAutoturisme = lista1.filtreazaAutoturisme()
  169. #print(isinstance(autoturism2, Autoturism))
  170. print(listaAutoturisme)
  171.  
  172. print("Camioane:")
  173. listaCamioane = lista1.filtreazaCamioane()
  174. print(listaCamioane)
  175.  
  176. #print(type(lista1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement