Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8891631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:47:39+00:00 2026-06-14T22:47:39+00:00

I’m a first year computer science student. I am trying to teach myself hash

  • 0

I’m a first year computer science student. I am trying to teach myself hash tables for an interview. After reading a few pieces about them, I thought the best way of seeing if I’d got it would be to implement my own hash table in Python. So that’s what I’ve done. Please could somebody look at it and let me know what you think? Have I correctly understood what it is I am meant to be doing with a hash table?

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:47:41+00:00Added an answer on June 14, 2026 at 10:47 pm

    It looks like you have the general concept correct. The hash table takes an arbitrary key and turns it into an index into an array via some special method.

    A couple points:

    First, and most important: your generate_hash function can return an index that is invalid if the sum of the ord()s of the key is greater than 1000.

    To fix this, have generate_hash return ascii_count % 1000. If you don’t know what % means, go read up on the modulus operator (don’t worry, it’s not too complicated).

    Second, also important: think about what happens if you use the following two keys: ab and ba. What you’re doing isn’t necessarily wrong, but it’s important to understand the behavior of your hash table when different keys collide.

    Third, less important: your for loops don’t have to work like they do in C/C++. You could change

    for character_index in range(0,len(character_list)):
            ascii_count += ord(character_list[character_index])
    

    to

    for character in character_list:
            ascii_count += ord(character)
    

    Python for loops are pretty fancy 🙂

    All in all, it looks great!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am reading a book about Javascript and jQuery and using one of the
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.