When I call getMin in my test file like this: self.lst.insert(self.lst.getMin())
, I get the error AttributeError: 'SortedList' object has no attribute 'getMin'
. I have tried to make a separate variable and then call getMin, but I get the same error. I imported the class correctly as my other methods work except for getMin and getMax, and I am wondering if I am either calling it incorrectly or there is some sort of spacing error. Here is my code:
def identity(x):
return x
class Link(object):
def __init__(self, value, next=None):
self.__data = value
self.__next = next
def getData(self):
return self.__data
def setData(self, value):
self.__data = value
def getNext(self):
return self.__next
def setNext(self, link):
if link is None or isinstance(link, Link):
self.__next = link
else:
raise Exception("Next link must be Link or None")
class SortedList(object):
def __init__(self, key=identity):
self.__first = None
self.__key = key
def getFirst(self):
return self.__first
def setFirst(self, link):
if link is None or isinstance(link, Link):
self.__first = link
else:
raise Exception("First link must be a Link or None")
def __len__(self):
l = 0
link = self.getFirst()
while link is not None:
l += 1
link = link.getNext()
return l
def find(self, goal_key):
link = self.getFirst()
while link is not None and self.__key(link.getData()) < goal_key:
link = link.getNext()
return link
def search(self, goal_key):
link = self.find(goal_key)
if link is not None and self.__key(link.getData()) == goal_key:
return link.getData()
def traverse(self, function=print):
current = self.__first
while current is not None:
function(current.getData())
current = current.getNext()
def insert(self, new_data):
new_link = Link(new_data)
if self.__first is None or self.__key(new_data) < self.__key(self.__first.getData()):
new_link.setNext(self.__first)
self.__first = new_link
else:
previous = None
current = self.__first
while current is not None and self.__key(new_data) >= self.__key(current.getData()):
previous = current
current = current.getNext()
new_link.setNext(current)
previous.setNext(new_link)
def delete(self, goal_key):
if self.__first is None:
raise Exception("No data with matching goal key found in the sorted list.")
if self.__key(self.__first.getData()) == goal_key:
self.__first = self.__first.getNext()
return
previous = None
current = self.__first
while current is not None and self.__key(current.getData()) != goal_key:
previous = current
current = current.getNext()
if current is None:
raise Exception("No data with matching goal key found in the sorted list.")
previous.setNext(current.getNext())
def getMin(self):
if self.__first is None:
return None
return self.__first.getData()
def getMax(self):
current = self.__first
while current.getNext() is not None:
current = current.getNext()
return current.getData()