class HashTable:
def __init__(self, size=5):
self.size = size
self.table = [[] for _ in range(size)]
def hash_function(self, key):
return hash(key) % self.size
def insert(self, key, value):
index = self.hash_function(key)
# Check if key exists and update
for i, (k, v) in enumerate(self.table[index]):
if k == key:
self.table[index][i] = (key, value)
return
# Otherwise, append new key-value
self.table[index].append((key, value))
def search(self, key):
index = self.hash_function(key)
for k, v in self.table[index]:
if k == key:
return v
return None
# Example usage
ht = HashTable()
ht.insert('apple', 10)
ht.insert('banana', 20)
ht.insert('grape', 30) # Might collide with 'apple'
print(ht.search('apple'))
print(ht.search('banana'))
print(ht.search('grape'))
print(ht.search('orange'))