Skip to content

Nicole-Lopez/python-hash-table

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Hash Table (Python)

This project is part of the freeCodeCamp Python Certification.

It implements a basic hash table data structure. The application stores key-value pairs, handles collisions internally, and provides efficient methods for adding, retrieving, and removing data.


πŸ“Œ Features

  • Store key-value pairs using a custom hash function
  • Convert string keys into numeric hash values
  • Handle collisions using nested dictionaries (bucket system)
  • Add new elements to the hash table
  • Retrieve values by key (lookup)
  • Remove elements by key
  • Return None when a key is not found

πŸ› οΈ Technologies

  • Python 3

πŸ“– User Stories

  1. You should define a class named HashTable with a collection attribute initialized to an empty dictionary when a new instance of HashTable is created. The collection dictionary should store key-value pairs based on the hashed value of the key.

  2. The HashTable class should have four instance methods: hash, add, remove, and lookup.

  3. The hash method should:

    • Take a string as a parameter.
    • Return a hashed value computed as the sum of the Unicode (ASCII) values of each character in the string. You can use the ord function for this computation.
  4. The add method should:

    • Take two arguments representing a key-value pair, and compute the hash of the key.
    • Use the computed hash value as a key to store a dictionary containing the key-value pair inside the collection dictionary.
    • If multiple keys produce the same hash value, their key-value pairs should be stored in the existing nested dictionary under the same hash value.
  5. The remove method should:

    • Take a key as its argument and compute its hash.
    • Confirm if the key exists in the collection.
    • Remove the corresponding key-value pair from the hash table.
    • If the key does not exist in the collection, it should not raise an error or remove anything.
  6. The lookup method should:

    • Take a key as its argument.
    • Compute the hash of the key, and return the corresponding value stored inside the hash table.
    • If the key does not exist in the collection, it should return None.

πŸ§ͺ Example Usage

Example test cases are included as commented lines at the bottom of main.py.

Uncomment them to run and see the output.


πŸ“š Notes

  • This project was developed to meet the certification requirements.
  • The implementation focuses on correctness and clarity rather than extended functionality.

About

freeCodeCamp Python certification project: Hash Table

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages