from random import randint class Plante: def __init__(self, art, navn, h?yde): self._vitenskapelig_navn = art + " " + navn self._hoyde = h?yde def hent_art(self): return self._vitenskapelig_navn.split(" ")[0] def hent_h?yde(self): return self._hoyde def __str__(self): return f"{self._vitenskapelig_navn}, {self._hoyde} cm" def __eq__(self, annen): if annen == None: return False return self.hent_art() == annen.hent_art() class Bed: def __init__(self, nr): self._bednr = nr self._bed_art = "" self._planter = [] def gro_plante(self, art, navn, h?yde): if self._bed_art == "": self._bed_art = art self._planter.append(Plante(art, navn, h?yde)) return True if art != self._bed_art: return False ugress_sjanse = randint(1, 4) if ugress_sjanse == 1: plante = Plante("Ugress", "", h?yde) else: plante = Plante(art, navn, h?yde) self._planter.append(plante) return True def luk_bed(self): ugress_ind = 0 while ugress_ind < len(self._planter): if self._planter[ugress_ind].hent_art() == "Ugress": self._planter.pop(ugress_ind) return True ugress_ind += 1 return False def hent_alle_planter(self): return self._planter def finn_h?yeste_plante(self): if len(self._planter) == 0: return None h?yeste = self._planter[0] for plante in self._planter: if (plante.hent_art() != "Ugress" and plante.hent_h?yde() > h?yeste.hent_h?yde()) or h?yeste.hent_art() == "Ugress": h?yeste = plante if h?yeste.hent_art() == "Ugress": return None return h?yeste def __str__(self): plante_streng = "" for plante in self.hent_alle_planter(): plante_streng += str(plante) + "\n" bed_streng = f"Bed {self._bednr}\nArt: {self._bed_art}\n" if plante_streng == "": bed_streng += f"Planter: \n{plante_streng}\n" if self.finn_h?yeste_plante() != None: bed_streng += f"H?yeste plante: \n{str(self.finn_h?yeste_plante())}\n" return bed_streng class Hage: def __init__(self, ant_bed): self._bed = [] for i in range(ant_bed): nytt_bed = Bed(i + 1) self._bed.append(nytt_bed) def legg_til_plante(self, art, navn, h?yde): for bed in self._bed: if bed.gro_plante(art, navn, h?yde): return print("Ingen bed passa") def luk_alle_bed(self, ant_ganger): antall_luket = 0 for bed in self._bed: while bed.luk_bed(): antall_luket += 1 if antall_luket == ant_ganger: return print(f"Fikk bare luket {antall_luket} ugress fra hagen") def finn_aller_h?yeste_plante(self): h?yest = self._bed[0].finn_h?yeste_plante() for bed in self._bed: bed_h?yeste = bed.finn_h?yeste_plante() if (bed_h?yeste != None and bed_h?yeste.hent_h?yde() > h?yest.hent_h?yde()) or (h?yest == None and bed_h?yeste != None): h?yest = bed_h?yeste if h?yest == None: print("Alle bedene var tomme, eller hadde kun ugress") return h?yest h = Hage(3) h.legg_til_plante("Canis", "Vulpis", 80) h.legg_til_plante("Canis", "Vulpis", 80) h.legg_til_plante("Canis", "Vulpis", 80) h.legg_til_plante("Canis", "Vulpis", 80) h.legg_til_plante("Canis", "Vulpis", 80) h.legg_til_plante("Canis", "Vulpis", 80) h.legg_til_plante("Canis", "Vulpis", 80) h.legg_til_plante("Canis", "Vulpis", 80) h.legg_til_plante("Caniss", "Vulpis", 80) h.legg_til_plante("Caniss", "Vulpis", 80) h.legg_til_plante("Caniss", "Vulpis", 80) h.legg_til_plante("Caniss", "Vulpis", 80) h.legg_til_plante("Caniss", "Vulpis", 900) h.legg_til_plante("Caniss", "Vulpis", 80) h.legg_til_plante("Caniss", "Vulpis", 80) h.legg_til_plante("Caniss", "Vulpis", 80) for bed in h._bed: for plante in bed.hent_alle_planter(): print(plante) print() h.luk_alle_bed(10) print("Etter luking:\n") for bed in h._bed: for plante in bed.hent_alle_planter(): print(plante) print() print(h.finn_aller_h?yeste_plante())