Assignment 9

1 minute read

Due: by the end of the calendar day on Wednesday, April 5, 2023

Assignment Requirements

Between what we’ve worked on in class as well as section 4.21 in the textbook (https://runestone.academy/ns/books/published/pythonds/BasicDS/ImplementinganUnorderedListLinkedLists.html), we have implemented most of the methods for the UnorderedList ADT (described here: https://runestone.academy/ns/books/published/pythonds/BasicDS/TheUnorderedListAbstractDataType.html).

For this assignment, you will implement one more: the index method. As the book notes:

index(item) returns the position of item in the list. It needs the item and returns the index. Assume the item is in the list.

Note that this should behave just like the Python list index method (though instead using your UnorderedList implementation instead) which can be used like this:

my_python_list = [54,26,93,17,77,31]
print( my_python_list.index(17) ) #displays 3 because 17 is at index 3 in the list

You then need to test it with code like this (though you should test more cases - does it work for the first item? last item? if there is only one item in the list?). Since the ADT description says you can assume the item is in the list, you can decide what happens when it is called for an item that isn’t in the list. Maybe it returns None or raises an Exception.

my_list = UnorderedList()

my_list.add(31)
my_list.add(77)
my_list.add(17)
my_list.add(93)
my_list.add(26)
my_list.add(54)

print( my_list.index(17) ) #displays 3 because 17 is at index 3 in the list

Note that index is a kind of search, so it should work similar too the search method you wrote (which is also in the book), but it should return the position in the list instead of True/False.

What to turn in

Put your UnorderedList class (and you probably need to define your Node class in the same file) in a file called linkedlist.py.

Include a comment that shows the testing that you did - that you called your methods and what the results were.

After thorough testing, submit your code to Assignment 9: Linked List index on codePost. I have included some automated test cases - so make sure to name your class and method as directed.

Grading

This is a 4-point assignment. See the rubric from the syllabus.