This is a partially implemented class called UnsortedMapOfLists, which extends UnsortedTableMap. The values associated with the keys in UnsortedMapOfLists are arrays. So you have an array with each key. Off course you can save an array in the value of UnsortedTableMap class without the class we are going to develop, but our class makes this easier, as it allows using a syntax like this: myMap["somekey", 1] to reach element at index 1 in the array associated with key "somekey". Methods __setitem__ and __getitem__ are already implemented. Your tasks are to: Implement and complete method __delitem__ . Deletion will be performed for an element in the array. If index is not given, delete the array associated with the key. Implement a function (not a method in the class) to swap the arrays associated with two given keys. Implement a main to verify that your code is working properly. You can decide how to do that.
This is a partially implemented class called UnsortedMapOfLists, which extends UnsortedTableMap. The values associated with the keys in UnsortedMapOfLists are arrays. So you have an array with each key.
Off course you can save an array in the value of UnsortedTableMap class without the class we are going to develop, but our class makes this easier, as it allows using a syntax like this: myMap["somekey", 1] to reach element at index 1 in the array associated with key "somekey". Methods __setitem__ and __getitem__ are already implemented. Your tasks are to:
- Implement and complete method __delitem__ . Deletion will be performed for an element in the array. If index is not given, delete the array associated with the key.
- Implement a function (not a method in the class) to swap the arrays associated with two given keys.
- Implement a main to verify that your code is working properly. You can decide how to do that.
UnsortedMapOfLists.py
from UnsortedTableMap import UnsortedTableMap
class UnsortedMapOfLists(UnsortedTableMap):
def __getitem__(self, position: tuple):
if len(position) == 1:
return super().__getitem__(position)
key, index = position
head: list = super().__getitem__(key)
if index >= len(head):
raise IndexError("Size of array associated with key: ", key, " is: ", len(head))
return head[index]
def __setitem__(self, position: tuple, value):
if len(position) == 1:
super().__setitem__(position, value)
return
key, index = position
try:
head: list = super().__getitem__(key)
if index < len(head):
# overrite exiting value, or append
head[index] = value
elif index == len(head):
head.append(value)
else:
raise IndexError("Size of array associated with key: ", key, " is: ", len(head))
except KeyError:
# new key, create a list for the key
head = [value]
self._table.append(self._Item(key, head))
def __delitem__(self, position: tuple):
"""Remove item associated with key k and index, if index not given, remove the item (array)
associated with the key
raise KeyError if not found
raise IndexError if index out of bound
"""
raise Exception("INSERT_TEXT_HERE")
m = UnsortedMapOfLists()
m["a", 0] = 5
m['a', 0] = 50
m['a', 1] = 60
m['b', 0] = 2
m['c', 1] = 10
m['d'] = [10, 20, 30]
print(m['a', 0])
print(m['a', 1])
print(m['b', 0])
print(m['a'])
print(m['d'])
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)