dsa.hashtable module — ucxdsa documentation

# dsa.hashtable module

Module containing hash table class.

class dsa.hashtable.HashTable(capacity=20)

  Bases: `object`

  A hashtable implementation.

  count

    the number of items in the hashtable

  enumerate()

    Return the enumeration of key-value pairs in the hashtable.

    Returns:

      Enumeration of key-value pairs.

  get(key)

    Get corresponding value of a given key in the hash table.

    Parameters:

      - key – The key to check for.

      - value – The value to set or create.

    Returns:

      corresponding value of key. None if key is not found.

  hash_function(key)

    Return a hash value based on a given key.

    Parameters:

      key – The key to convert to a hashvalue.

    Return type:

      `int`

    Returns:

      Hash value modded to the hashtable capacity.

  key_exists(key)

    Returns a Boolean on whether a key exists in the hashtable or not .

    Parameters:

      key – The key to check for in the hashtable.

    Return type:

      `bool`

    Returns:

      Boolean of key existence.

  pop(key, default=None)

    Remove specified key and return the value. If key is not found, return default.

  remove(key)

    Remove key-value pair if specified key is found. Raise KeyError if not found.

    Parameters:

      key – The key to check for.

    Raises:

      KeyError – If the key is not found in the hashtable.

  set(key, value)

    Set a key-value pair in the hashtable.

    If key exists, replace the value otherwise, create a new key-pair.

    Parameters:

      - key – The key to check for.

      - value – The value to set or create.

  show_buckets()

    Return a string displaying the contents of all buckets in the hashtable.