0

So I am trying to get a grasp on Hash Functions and how exactly they work. I have the following code but I keep getting an error when I try and run the code.

import sys 

def part_one():   

        foo = open('input_table.txt')
        for line in foo:
            id, make, model, year = line.split(",")
            print(make, model) 
            tuple_list = (make+model,)
        return tuple_list



def hash_one(num_buffers, tuple_list):
        #part_one()
    # A being the first constant prime number to multiply by
    # B being the prime number that we add to A*sum_of_chars
        tuple_list = part_one()
        A = 3
        B = 5
        count = 0 

        for item in tuple_list:
            for char in item:
        # sum_of_chars is the total of each letter in the word
                count = ord(char)
                count = count + tuple_list
        index = ((A * sum_of_chars + B)) % num_buffers
        return index    



if __name__ == '__main__':

    input_table = sys.argv[1] 
    num_buffers = int(sys.argv[2])
    chars_per_buffer = int(sys.argv[3])
    sys.argv[4] = 'make'
    sys.argv[5] = 'model'
    lst = []
    for item in range(4, len(sys.argv)):
        lst.append(sys.argv[item])
    print(lst)
    hash_one(lst)

What is wrong with my code that is causing the error? Can anyone help me?

4
  • 1
    Can you also add the error that you are receiving? Commented Oct 26, 2014 at 19:09
  • Please say what error you are getting it, what line, etc. Commented Oct 26, 2014 at 19:09
  • It is giving me a typeerror line 48 & line 29 Commented Oct 26, 2014 at 19:21
  • 1
    There are no line numbers here. Include the complete traceback (error report). Commented Oct 26, 2014 at 19:32

1 Answer 1

1

1

You're calling hash() with no arguments, you have to hash something.

A hash of a number will just return the same number though, so it's not very interesting. It's for hashing things like strings.

2

part_one returns nothing, therefore when you call tuple_list = part_one(), it's value is set to None, and you can't iterate though it.

3

Passing in a list through an argument then overwriting it doesn't make any sense anyway. If you want to return a list then use a return statement.

4

It's odd to set argument variables in code, they're for reading things from the command line.

5

(Not an error, but...) You can use a slice (lst = sys.argv[4:]) as an easier way to get a sub-section of a list.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.