Assignment 2 - Hash Tables and Python Dictionaries

You have a similar error to what I had encountered in Question 2. Read the comments closely in the code block for Question 5 and make sure that is what you are doing. Keep in mind local/global variables as you do so.

1 Like

@fuzzyray Thanks for the suggestion. Will check it out.

This is a curious case of “oops”, termed as function overloading. Where same function name with different parameters exits.

Seems like by correcting Q2 as you suggests, I got my Q4 correct as well.

As far as I can tell, Python does not support Function/Method Overloading.

Once again thanks for your help.

Faced problems with Q5. Perils of copy+paste actually. When we blindly copy/paste the code. We tend to miss something very basic. Clever Aakash & Jovian team!

1 Like

Well, python doesn’t support it, but it can be done with decorators. Albeit it’s a bit hacky.

1 Like

Please help, getting error in Q3.

maybe try doing return kv[1] @parveenrohilla06. The error seems to say that it cannot unpack the object, so just deleting key, value = kv and only have the return kv[1]

I think the hash() function is not always returning the values in a sorted fashion. For example, when I run hash('e') I get 6412827021040127549, but when I run hash('f'), (f is bigger than e), I get the following big negative number: -1307993023915369992

in Q5 I’m getting a False statement, but there is no error
can you check my this function is correct?

    # If the stored key matches the given key, return the index
    k, v = kv
    if k == data_list[idx] :
        return idx

What’s wrong here

def get_valid_index(data_list, key):
# Start with the index returned by get_index
idx = get_index(data_list, key)

while True:
    # Get the key-value pair stored at idx
    kv = data_list[idx]
    # If it is None, return the index
    if kv == None:
        return idx
    # If the stored key matches the given key, return the index
    k, v = kv
    if k == data_list[idx] :
        return idx
    # Move to the next index
    idx += 1
    # Go back to the start if you have reached the end of the array
    if idx == len(data_list):
        idx = 0

The comment stated quite clearly that you need to return the index if the stored key MATCHES (this is the main point) the given key, that’s what I could suggest, thanks.

Finally I did it :slight_smile: Thank you

I got my number 5 question all false answer, what did you do different from the 3 question. thanks in advance

Can somebody please tell me what’s going on in here?

My Q1 to Q4 seems to be working good & all came TRUE…but Q5 is showing FALSE! if the key and value get inserted okay then why would i not be getting the value of key “listen”? cause they both use the same get_valid_index function!!

1 Like

Hello everyone, I added a function that scrambles the key into nonsense characters. if anyone wants to have a look and improve it or has any other weird functions please comment!
def name_changer(key):
new_name = [chr for chr in key]
key = ’ ’
for x in new_name:
x = ord(x)+ 3
if chr(x) == ‘#’:
x = ord(’ ')
key+= chr(x)
return key
key= name_changer(key)
probing_table.update(key, value)

1 Like

I actually had the exact same problem, all the outputs were True in q4, but false in q5. So the problem is within your get valid index function. It is at the if statement that is checking to see if you move on the next index. Make sure that the if statement is as follows if k == key: Hope this helps and have a good day


You use the get_valid_index function instead of the get index function. But if you used that, there is most likely a problem with your get valid index function.

Alright, thanks a lot