Skip to content

Hashtable - Linear probing __setitem__ not considering updates. #78

Open
@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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy