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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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:

  1. 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.
  2. Implement a function (not a method in the class) to swap the arrays associated with two given keys.
  3. 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'])

 

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY