English subtitles

← Finding Buckets Solution - Intro to Computer Science

Get Embed Code
2 Languages

Showing Revision 5 created 05/24/2016 by Udacity Robot.

  1. Here's the answer; we can define hashtable_get_bucket, and
  2. it takes two inputs, the hash table, we'll
  3. call that htable, and the key, which is
  4. the word we're looking for. And to find the
  5. bucket, well, we're going to use hash_string. We're going to
  6. pass in the same word, the keyword, that's the
  7. input key. The number of buckets is the
  8. length of this table, so we're going to call hash_string,
  9. passing in the key. And as the second input, we need
  10. the length of the table, that's the number of buckets. So
  11. that will get us a number which is the index of
  12. the bucket we want. To get that bucket, we need to
  13. use that as the index to select that element from htable,
  14. and then we want to return the results. So that's all
  15. we need to find the bucket. Let's look at that in
  16. the Python interpreter. So here's the code we have so far.
  17. We have the hash string procedure we defined that maps
  18. the key word and a number of buckets to the position
  19. where that should occur in the hash table. We have
  20. the make_hashtable procedure that creates an empty table with that number
  21. of buckets. And now we have the hashtable_get_bucket procedure that
  22. takes a hash table and a key. And give this the
  23. element of the hash table which is where that key would
  24. belong using that hash string function to find the right position.