Skip to content

Hashtable - Linear probing __setitem__ not considering updates. #78

@ManuMathewVarghese

Description

@ManuMathewVarghese

Case: Key exists after the first empty slot.
Issue: Code inserts the new (key,value) instead of updating existing (key,value).

Code in question:
def find_slot(self, key, index):
        prob_range = self.get_prob_range(index)
        for prob_index in prob_range:
            if self.arr[prob_index] is None:
                return prob_index
            if self.arr[prob_index][0] == key:
                return prob_index
        raise Exception("Hashmap full") ```
Maybe this can fix it:
def find_slot(self, key, index):
        first_slot = None
        prob_range = self.get_prob_range(index)
        for prob_index in prob_range:
            if first_slot == None and self.arr[prob_index] is None:
                first_slot = prob_index
            if self.arr[prob_index][0] == key:
                return prob_index
        if first_slot is not None:
           return first_slot
        raise Exception("Hashmap full")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions