Question: Implement a map in PYTHON using a sorted list of entries. You should keep the entry list sorted by key at all times, and make
Implement a map in PYTHON using a sorted list of entries. You should keep the entry list sorted by key at all times, and make use of the binary search routine included in the scaffold to speed up key lookups.
The binary search function is : def binary_search(self, key): min = 0 max = len(self._entries) while min < max: mid = int((max + min)/2) mid_key = self._entries[mid].get_key() if mid_key == key: return mid elif mid_key < key: min = mid + 1 else: max = mid # not found return -1
the functions we need to implement using this are:
containsKey(k)
: Returns true if there exists an entry with key k in the map, false otherwise.
get(k)
: If the map contains an entry with key k and value v, returns v. Otherwise
returns null.
put(k, v)
: Inserts an item with with key k and value v; if there is already an item with
key k, then replaces its value with v.
remove(k)
: If the map contains an entry with key k and value v, removes said entry and
returns v. Otherwise returns null.
You can't use the built-in Python functions
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
