我是一年級計算機專業的學生的理解。我正試着自己教一個散列表來面試。在閱讀了關於它們的幾篇文章之後,我認爲看看我是否能夠實現它的最好方法是在Python中實現我自己的散列表。這就是我所做的。請有人看看它,讓我知道你的想法?我是否正確地理解了我想要用哈希表做什麼?檢查我的哈希表
storage_array = []
def show_menu():
menu_option = int(raw_input("Enter 1 to store data, Enter 2 to retrieve data: "))
if (menu_option == 1):
store_data()
elif (menu_option == 2):
retrieve_data()
def store_data():
key_for_data = raw_input("Please enter the key for the data you want to store: ")
actual_data = raw_input("Please enter the data you want to store: ")
ascii_count = generate_hash(key_for_data)
print ascii_count
storage_array[ascii_count] = actual_data
print "The data:'", actual_data, "'has been stored at index:'", ascii_count, "' which is the ascii count of:'", key_for_data, "'"
show_menu()
def retrieve_data():
key_for_data = raw_input("Enter the key for the data you want to retrieve: ")
ascii_count = generate_hash(key_for_data)
if (storage_array[ascii_count] == None):
print "No data was stored under this key"
else:
print "The data you requested for key:'", key_for_data, "'with ASCII count:'", ascii_count, "' is:'", storage_array[ascii_count], "'"
show_menu()
def generate_hash(input):
character_list = list(input)
ascii_count = 0
for character_index in range(0,len(character_list)):
ascii_count += ord(character_list[character_index])
return ascii_count
def initiate_list():
for repeat_index in range(0,1000):
storage_array.append(None)
print "List initiated with index's to 1000"
initiate_list()
show_menu()
##Or is it meant to hash the key like a dictionary and then store
##the value for that key in the hashed value in the hash table?
請發佈代碼,不要發佈屏幕截圖。 – alex
爲什麼?屏幕截圖不僅僅可以用來檢查對概念的理解嗎? –
您發佈了一張文字圖片。改爲發佈文字。 – vicvicvic