Created
October 29, 2019 21:06
-
-
Save feulf/8217c58dc6439f4cd4712ba3e197637a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
hashed_list = [None] * 10 # create an array of None | |
def key_position(key): | |
string = key.encode() # convert to bytes | |
hashed_key = hashlib.sha256(string) # hash the string | |
hex_digest = hashed_key.hexdigest() # get the hex value | |
int_digest = int(hex_digest, 16) # convert to int | |
return int_digest % len(hashed_list) # run the modulo of the length of the array | |
def insert(key, value): | |
index = key_position(key) | |
hashed_list[index] = value | |
def read(key): | |
index = key_position(key) | |
return hashed_list[index] | |
insert("Satoshi Nakamoto", "Unknown") | |
insert("Federico", "5.7") | |
insert("Joe", "6.1") | |
print(read("Satoshi Nakamoto")) | |
print(read("Federico")) | |
print(read("Joe")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment