# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.7 (tensorflow)
#     language: python
#     name: tensorflow
# ---

# # Implementation of a Hash Table
#
# In this lecture we will be implementing our own Hash Table to complete our understanding of Hash Tables and Hash Functions! Make sure to review the video lecture before this to fully understand this implementation!
#
# Keep in mind that Python already has a built-in dictionary object that serves as a Hash Table, you would never actually need to implement your own hash table in Python.

# # Map
#
# The idea of a dictionary used as a hash table to get and retrieve items using keys is often referred to as a mapping. In our implementation we will have the following methods:
#
#  <li> HashTable() Create a new, empty map. It returns an empty map collection.
#  <li> put(key,val) Add a new key-value pair to the map. If the key is already in the map then replace the old value with the new value.
#   <li> get(key) Given a key, return the value stored in the map or None otherwise.
#   <li> del Delete the key-value pair from the map using a statement of the form del map[key].
#   <li> len() Return the number of key-value pairs stored
#   <li> in the map in Return True for a statement of the form key in map, if the given key is in the map, False otherwise.
#

# +
class HashTable(object):

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

    def put(self,key,data):
        hashvalue = self.hashfunction(key,len(self.slots))
        if self.slots[hashvalue] == None:
            self.slots[hashvalue] = key
            self.data[hashvalue] = data

        elif self.slots[hashvalue] == key:
            self.data[hashvalue] == data
        
        else:
            nextslot = self.reshash[hashvalue,len(self.slots)]

            while self.slots[nextslots] != None and self.slots[nextslot] != key:
                nextslot = self.rehash(nextslot,len(self.slots))
            if self.slots[nextslot] == None:
                self.slots[nextslots] = key
                self.data[nextslot] = data

            else:
                self.data[nextslot] = data
    
    def hashfunction(self,key,size):
        # The actual hash function
        return key%size
    
    def reshash(self,oldhash,size):
        return (oldhash+1)%size
    

    def get(self,key):
        
        # Getting items given a key
        
        # Set up variables for our search
        startslot = self.hashfunction(key,len(self.slots))
        data = None
        stop = False
        found = False
        position = startslot
        
        # Until we discern that its not empty or found (and haven't stopped yet)
        while self.slots[position] != None and not found and not stop:
            
            if self.slots[position] == key:
                found = True
                data = self.data[position]
                
            else:
                position=self.rehash(position,len(self.slots))
                if position == startslot:
                    
                    stop = True
        return data

    # Special Methods for use with Python indexing
    def __getitem__(self,key):
        return self.get(key)

    def __setitem__(self,key,data):
        self.put(key,data)
    

# -

h = HashTable(5)

h[1] = 'one'

h[2] = 'two'

h[3] = 'three'

h[1]

print(h[5])

h[2] = 'newtwoo'

h[2]



StarcoderdataJupyterScriptsDedupFiltered