Advertisement
Poganu

Untitled

Oct 9th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.53 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. class Camion(Vehicul):
  44.     def __init__(self, nrImatriculare, anInmatriculare, tonaj, nrRemorci):
  45.         super().__init__(nrImatriculare, anInmatriculare)
  46.         self.tonaj = tonaj
  47.         self.nrRemorci = nrRemorci
  48.  
  49.     def __repr__(self):
  50.         return "Camionul " + str(self.nrInmatriculare) + " " + str(self.anInmatriculare) + \
  51.             " " + str(self.tonaj) + " " + str(self.nrRemorci)
  52.  
  53. class Lista():
  54.     def __init__(self):
  55.         self.root = None
  56.  
  57.     def adauga(self, element):
  58.         if element != None: #daca elementul nu este gol
  59.             # in cazul in care adaugi primul element
  60.             if self.root == None:
  61.                 self.root = element
  62.                 self.link = None
  63.             # in cazul in care adaugi in mijlocul listei
  64.             elif (element > self.root):
  65.                 succesor = None
  66.                 predecesor = None
  67.                 x = self.root # variabila locala
  68.                 while element > x.getLink() and x.getLink() != None:
  69.                     x = x.link
  70.                 if x.link == None: #adaugare element la capat
  71.                     x.link = element
  72.                 else: #adaugare in pozitie intermediara
  73.                     succesor = x.link
  74.                     predecesor = x
  75.                     element.link = succesor
  76.                     predecesor.link = element
  77.             else:
  78.                 element.link = self.root
  79.                 self.root = element
  80.             # in cazul in care adaugi la capatul listei
  81.  
  82.  
  83. # nr           1      2    3    4    5
  84. # root  None  None    1    2    3    4
  85. # link  1      2      3    4    5    6
  86.  
  87.  
  88.     def __repr__(self):
  89.         s = " "
  90.         i = self.root
  91.         while i!=None:
  92.             s = s + str(i) + " "
  93.             print(i)
  94.             i = i.link
  95.         return s
  96.  
  97.  
  98.  
  99. autoturism1 = Autoturism("CJ01TRE", 2004, 100, 4)
  100. print(autoturism1)
  101.  
  102. camion1 = Camion("B01BMW", 2002, 80, 2)
  103. print(camion1)
  104.  
  105. print("Este mai mare: ")
  106. if camion1 > autoturism1:
  107.     print(camion1)
  108.  
  109. print("-----------------")
  110. lista1 = Lista()
  111. lista1.adauga(autoturism1)
  112. lista1.adauga(camion1)
  113. print(lista1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement