knapscak and graph optimization problems the only interesting code is the implementation of the function greedy by introducing the parameter keyfunctionwe make greedy independent of the order in which the elements of the list are to be considered all that is required is that keyfunction defines an ordering on the elements in items we then use this ordering to produce sorted list containing the same elements as items we use the built-in python function sorted to do this (we use sorted rather than sort because we want to generate new list rather than mutate the list passed to the function we use the reverse parameter to indicate that we want the list sorted from largest (with respect to keyfunctionto smallest class item(object)def __init__(selfnvw)self name self value float(vself weight float(wdef getname(self)return self name def getvalue(self)return self value def getweight(self)return self weight def __str__(self)result '<self name 'str(self value)'str(self weight'>return result def value(item)return item getvalue(def weightinverse(item)return /item getweight(def density(item)return item getvalue()/item getweight(def builditems()names ['clock''painting''radio''vase''book''computer'values [ , , , , , weights [ , , , , , items [for in range(len(values))items append(item(names[ ]values[ ]weights[ ])return items figure building set of items with orderings