Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added delete function in separate copy of code #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 97 additions & 1 deletion computing_sh/Python11_Hash_Table_Search.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,100 @@
]
}
]
}
}


# hash function
def hash(key):
return key % N

def insert(key, data):
index = hash(key)
if hash_table[index] == -1: # available
hash_table[index] = data
else: # collision
i = index + 1
while hash_table[i] != -1:
i += 1
hash_table[i] = data

def search(key, data):
index = hash(key)
if hash_table[index] == data: # found
return "BINGO!"
elif hash_table[index] == -1:
return "Not found"
else: # collided records
i = index + 1
if i<len(hash_table):
while hash_table[i] != -1:
if hash_table[i] == data: # found in collided records
return "FINALLY!"
else:
i += 1
return "Unfortunately"

def delete(key, data):
if search(key, data) == "BINGO!": # foudn in uncollided records
index = hash(key) # find the index
hash_table[index] = -1
return "Deleted (Non-Collision)"
elif search(key, data) == "FINALLY!": # collided records
index = hash(key) # find the index
i = (index + 1) % N
while hash_table[i] != -1:
if hash_table[i] == data:
hash_table[i] = -1
return "Deleted (Collision)"
else:
return "Not Deleted (Not found)"

# insert
records = {991: "Tom", 992: "Mary", 993: "Ali"} # insert without collision
for key, value in records.items(): #.items() function needed because records is in a dictionary
insert(key, value)
insert(1003, "Peter") # insert with collision
print(hash_table)

# main
N = 10
hash_table = [-1 for i in range(N)]
print(hash_table)

records = {1234: "Tom", 321: "Mary", 659: "Ali"}
print(records)

# insert without collision
for key, value in records.items():
insert(key, value)
print(hash_table)

# insert with collision
insert(784, "Peter")
print(hash_table)

# search without collision
print(search(659, "Ali")) # successful
print(search(882, "Lucy")) # unsuccessful

# search with collision
print(search(784, "Peter")) # successful
print(search(404, "John")) # unsuccessful

# update without collision
print(update(659, "Ali", "Ali2")) # successful
print(update(321, "Ahmad", "Ahmad2")) # unsuccessful

# update with collision
print(update(784, "Peter", "Peter2")) # successful
print(update(784, "Roy", "Roy2")) # unsuccessful

# delete without collision
print(delete(321, "Mary"))

# delete with collision
print(delete(784, "Peter2"))

# unsuccessful deleted (with and without collision)
print(delete(999, "Bala"))
print(hash_table)