user5405648
user5405648

Reputation:

Hashing a string in python returns the wrong result?

I've been set a homework task by a tutor to create a procedure which hashes a string, or in a sense gives the index of the string in a hash table if it was hashed.

It was supposed to return 11, but returned 0, can someone help me figure out why?

def hash_string(keyword, buckets):
    ords = []
    for e in string_to_list(keyword):
        ords.append(ord(e))

    sum_of_ords = ords.pop()

    for e in ords:
        sum_of_ords = sum_of_ords * e

    return sum_of_ords % buckets

print(hash_string('udacity', 12)) # should return 11 but returns 0?

Here is the string_to_list, I know theres probably a better way, but this is the only way I knew how without using google to search a built in method for this type of thing

def string_to_list(str):
    result_list = []
    i = 0

    while i < len(str):
        result_list.append(str[i:i + 1])
        i += 1


    return result_list

Here is how my tutor described the answer, but I don't undersand what he is doing with h? Is this just a simplified version of what I'm trying to do?

def hash_string(keyword, buckets):
    h = 0
    for c in keyword:
        h = (h + ord(c)) % buckets
    return h

Upvotes: 1

Views: 173

Answers (2)

Hybrid
Hybrid

Reputation: 7049

The issue is that you used a few unnecessary functions, like using .pop(), a for loop, and your hash_string function can be replaced by using list(<string>).

Example:

def hash_string(keyword, buckets):
    ords = []
    for e in list(keyword):
        ords.append(ord(e))

    return sum(ords) % buckets

This can be even further simplified to:

def hash_string(keyword, buckets):
    return sum([ord(char) for char in list(keyword)]) % buckets

Upvotes: 0

iz_
iz_

Reputation: 16573

It seems like you meant to add but multiplied by accident here:

sum_of_ords = sum_of_ords * e

Change this to:

sum_of_ords = sum_of_ords + e

Or, with compound assignment:

sum_of_ords += e

On a side note, you can greatly simplify your function to simply this:

def hash_string(keyword, buckets):
    return sum(ord(c) for c in keyword) % buckets

Upvotes: 2

Related Questions