Yes, as @MRAB says, the part that says node = BookCollectionNode(book) was right. The issue is in the node > self.head and current.getNext() > node parts. You’re comparing the nodes themselves, but you actually want the Book that’s in the data attribute of each of them.
Now, when it gets to this line:
data = current.getData() + "\n"
It will tell you that + doesn’t support the types Book and str. That’s because current.getData() just gets you the Book. You’ll need to get a string with the information in the proper format from that. (See your getBookDetails method.) And—again, as @MRAB mentioned—you can’t just return a list, but you’ll need to combine the strings into one large string.
this revised BookCollection.py file makes this error message: Autograder Results
1.Your submission timed out. It took longer than 600 seconds to run.
from BookCollectionNode import BookCollectionNode
from Book import Book
class BookCollection:
def __init__(self):
self.head = None
def isEmpty(self):
return self.head == None
def getNumberOfBooks(self):
count = 0
current = self.head
while current != None:
count += 1
current = current.getNext()
return count
def insertBook(self, book):
node = book
if self.isEmpty():
self.head = node
elif node > self.head:
node.setNext(self.head)
self.head = node
else:
current = self.head
while current.getNext() is not None and current.getNext() > node:
current = current.getNext()
node.setNext(current.getNext())
current.setNext(node)
def getBooksByAuthor(self, author):
result = []
current = self.head
while current != None:
if current.getData().getAuthor().lower() == author.lower():
data = current.getData() + "\n"
result.append(data)
current = current.getNext()
return result
def getAllBooksInCollection(self):
result = []
current = self.head
while current != None:
data = current.getData() + "\n"
result.append(data)
current = current.getNext()
return result
def removeAuthor(self, author):
author = author.lower()
current = self.head
previous = None
while current is not None:
if current.getData().getAuthor().lower() == author:
if previous is None:
# Removing the head node
self.head = current.getNext()
else:
previous.setNext(current.getNext())
# Move the current pointer to the next node
current = current.getNext()
else:
# Move the previous and current pointers to the next node
previous = current
current = current.getNext()
def recursiveSearchTitle(self, title, bookNode):
if bookNode is None:
return False
elif bookNode.getData().getTitle().lower() == title.lower():
return True
else:
return self.recursiveSearchTitle(title, bookNode.getNext())
Get the code all running and working on your own machine, before submitting it to the autograder.
Write more unit tests – ideally you have tests for each bit of the code, rather than jsut tests for teh top level functions – for instance, does your __gt__ method work correctly? – do two books compare the way they should?
while working on your own machine, figure out how to run just one failing test at a time – it’s a lot easier to focus on one problem at a time.
Good luck – you’re getting there, you just need to keep doing more of the same.
If it’s timing out, it’s probably stuck in a loop somewhere. Add more tests. I’d suggest, say, testing the code for insertion by having a set of 3 books and trying inserting them into empty book collections in different orders.
I acknowledge your request and concern, I will do my own homework.
I’m only really here in college just to get my degree to unlock a job that will earn me a lot of money and data/compsci is the latest, greatest cash cow.