This assignment comes with several starter files but focus on the following files: ps1.py, ps2.py, ps3_4.py, and testbag.py.
- Use ps1.py for question 1, ps2.py for question 2, and ps3_4.py for questions 3 and 4.
- You can run your code for questions 1 and 2 together.
- To test your answers for questions 3 and 4, run testbag.py.
You’ll need to grab the assignment from the link below and download the entire folder to your computer before proceeding. I would highly suggest then opening the entire folder in VS Code, as it will make it very easy to edit the different files and is necessary for VS Code to find certain libraries. When you are finished, upload your completed templates back to GitHub. Don’t worry about changing any file names, you want to overwrite the original template files.
Accept AssignmentThese questions require you to define and test functions for manipulating linked structures and array-based bags. Use the Node and TwoWayNode classes as defined in class (already included in the starter files node.py).
Question 1
Define a function length (not len) that expects a singly linked structure as an argument. The function returns the number of items in the structure.
Question 2
Define a function named insert that inserts an item into a singly linked structure at a given position. The function expects three arguments: the item, the position, and the linked structure. (The latter may be empty.) The function returns the modified linked structure. If the position is greater than or equal to the structure’s length, the function inserts the item at its end.
An example call of the function, where head is a variable that either is an empty link or refers to the first node of a structure, is:
head = insert(1, data, head)
Question 3
Complete the code for the ArrayBag method add, so that the array is resized when necessary.
Question 4
Complete the code for the ArrayBag method remove, so that the array is resized when necessary.