# Oppgave 16 (4 poeng) # med forslag til poengsetting class Vare: # Eng. Product/Item def __init__(self, vid): # vid is product id/name (str) for this vare/product self._vareid = vid self._butikker = {} # 1 poeng def __str__(self): # NB ikke del av oppgaven 0p tekst = self._vareid + ': ' for b in self._butikker: tekst += b + ':' + str(self._butikker[b]) + " " return tekst def legg_til_butikk_og_pris(self, bnavn, pris): # adds information about shop (bname) and price # to the dictionary with this information self._butikker[bnavn] = pris # 1 poeng def vareid(self): # returns name/id of this vare/product (str) return self._vareid # 0,5 poeng def pris_i_butikk(self, butikkid): # Makes a lookup in the dictionary with shop and price information and # returns the price of this item in the store named shopid # if the store ID is not found in the dictionary, None is to be returned if butikkid in self._butikker: return self._butikker[butikkid] # 1 poeng else: return None # 0,5 poeng # Oppgave 17 Datastruktur med vareobjekter (3 poeng) # med forslag til poengsetting def opprett_datastruktur(priser): ordbok = {} # 0,5 p for kortliste in priser: # 0,5 p vareid = kortliste[1] butikkid = kortliste[0] pris = kortliste[2] if not (vareid in ordbok): nyvare = Vare(vareid) # 1p ordbok[vareid] = nyvare vare = ordbok[vareid] # vareobjektet vare.legg_til_butikk_og_pris(butikkid, pris) # 1p return ordbok # Test (ikke oppg): db = [ ["Coop", "Norvegia 1kg", 104.99], ["Kiwi", "Blomk?l", 24.90],["Coop", "Blomk?l", 21.90] ] datastr = opprett_datastruktur(db) for i in datastr: print(datastr[i]) print(datastr)