Q5 of Assignment 2

Hi,

What’s wrong with the implementation below? Can you help me, please?

If I used “get_valid_index(data_list, key)” instead of “get_valid_index(self.data_list, key)” I obtained TRUE | FALSE | TRUE | FALSE.

I am very grateful to you for your help.

p/s: It seems that the last version of Assignment 2 used get_index(data_list, key) and get_valid_index(data_list, key), isn’t it?


class ProbingHashTable:
def init(self, max_size=MAX_HASH_TABLE_SIZE):
# 1. Create a list of size max_size with all values None
self.data_list = max_size * [None]

def insert(self, key, value):
    # 1. Find the index for the key using get_valid_index
    idx = get_valid_index(self.data_list, key)
    
    # 2. Store the key-value pair at the right index
    self.data_list[idx] = (key, value)


def find(self, key):
    # 1. Find the index for the key using get_valid_index
    idx = get_valid_index(self.data_list, key)
    
    # 2. Retrieve the data stored at the index
    kv = self.data_list[idx]
    
    # 3. Return the value if found, else return None
    return None if kv is None else kv[1]


def update(self, key, value):
    # 1. Find the index for the key using get_valid_index
    idx = get_valid_index(self.data_list, key)
    
    # 2. Store the new key-value pair at the right index
    self.data_list[idx] = (key, value)


def list_all(self):
    # 1. Extract the key from each key-value pair 
    return [kv[0] for kv in self.data_list if kv is not None]

This part of your code seems ok to me, Check the implementation of your get_valid_index() function.

Thank you, Sir.
The implementation of my get_valid_index() function seems OK. Because I tried to submit my Assignment 2 and I got a “PASS” for Q4.


Sans titre

Your code in the should be like this

k, v = kv
if k == key:
     return idx

k should match the value of key

1 Like

Thank you so much, Sir!
It worked :).

1 Like