Question
For this question you will need to complete two functions: The add_item() function. The remove_item() function. The add_item() function takes three parameters: An item to
For this question you will need to complete two functions:
- The add_item() function.
- The remove_item() function.
The add_item() function takes three parameters:
- An item to be added to the list passed as the second parameter. The item can be an object of any type.
- A list of items called item_list.
- An integer parameter max_size indicating the maximum number of items the item list may have.
If the items_list has less items than max_size, the item can be added to the end of the list. A message indicating that the addition was successful is printed. Otherwise, a message is printed indicating that the list is full. In this situation the list remains unchanged. Note that this function simply updates the parameter list. It does not create or return a new list.
The remove_item() function takes one parameter, a list of items called item_list. If item_list is not empty, an item is removed from the end of the list and a message is printed indicating that the removal of the item was successful. Otherwise, a message is printed indicating that the list is empty and that no item was removed. In this situation the list remains unchanged. Again, note that this function simply updates the parameter list. It does not create or return a new list.
Some examples of these functions being called are shown below.
For example:
Test | Result |
---|---|
item_list = [] print("Start:", item_list) add_item(5, item_list, 5) add_item(-21, item_list, 5) print("End:", item_list) | Start: [] 5 added to list. -21 added to list. End: [5, -21] |
item_list = [1, 2, 3, 4, 5] print("Start:", item_list) add_item(6, item_list, 5) remove_item(item_list) remove_item(item_list) add_item(25, item_list, 5) print("End:", item_list) | Start: [1, 2, 3, 4, 5] List is full. Item cannot be added. 5 has been removed from list. 4 has been removed from list. 25 added to list. End: [1, 2, 3, 25] |
item_list = [1, 2, 3] print("Start:", item_list) remove_item(item_list) remove_item(item_list) remove_item(item_list) remove_item(item_list) print("End:", item_list) | Start: [1, 2, 3] 3 has been removed from list. 2 has been removed from list. 1 has been removed from list. List is empty. Nothing to remove. End: [] |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started