1
class MyHashTable:

    def __init__(self, capacity):
        self.capacity = capacity
        self.slots = [None] * self.capacity

    def __str__(self):
        return str(self.slots )

    def __len__(self):
        count = 0
        for i in self.slots:
            if i != None:
                count += 1
        return count

    def hash_function(self, key):
        slot = key % len(self.slots)

        if key in self.slots:
            return slot

        elif (not key in self.slots) and len(self.slots) == self.capacity:
            return slot

        else:
            for i in self.slots:
                count = 0 
                if i == None:
                    return count
                count += 1

    def insert(self, key):
        print(len(self.slots)) #Why does this show an output of 2? 
        if key in self.slots:
            return -2

        elif (not key in self.slots) and (len(self.slots) != self.capacity): #Now this cant execute
            num = hash_function(key)
            self.slots[num] = key
            return num

        elif (not key in self.slots) and len(self.slots) == self.capacity:
            return -1

Im wondering why the commented part above in the insert(self, key) the print statement gives (2) instead of (0). The elif statement underneath wont execute since its giving a result of (2) instead of (0)

A function call of

x = MyHashTable(2)
print(len(x)) 

Should give: 0

1
  • You should not compare to None with ==. Instead you should test as e.g. if i is None, or if i is not None Commented Oct 5, 2015 at 10:15

2 Answers 2

1

You're initializing self.slots = [None] * self.capacity, so with capacity = 2, self.slots is [None, None], which is of length 2.

Your __len__ method doesn't run because len(self.slot) calls self.slot.__len__, not self.__len__. If you'd like to use your override method, you should be calling len(self) instead.

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

3 Comments

But what about def __len__(self): Shouldnt this override the len() function and thus give 2?
that override is for the class/object itself, not self.slot . doing len(self.slot) , would call that list's __len__() . I am guessing you wanted to do len(self) .
@WojackO'feel As Anand noted, the __len__ override is for your MyHashTable class, not for self.slot (which is an instance of list).
1

You have to call your __len__ function (by calling self.__len__() ) if you want the length of the elements which are not None. For lists None are valid entries.

By the way. It is always best to compare with None by a is None or a is not None instead of == or !=.

3 Comments

How exactly do I call the len function? I thought len() would be overrided, by the way why is "a is None" better than the == operator?
simply use self.__len__() or len(self) instead of len(self.slots)
Ah, you have overwritten the len-function but only for your class and not for the array you use for self.slots. That one is always referring to self.slots.__len__ and not to self.__len__. About the difference between == and is: python.org/dev/peps/pep-0008 (see the second point in the section about Programming Recommendations)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.