Here's the answer; we can define hashtable_get_bucket, and
it takes two inputs, the hash table, we'll
call that htable, and the key, which is
the word we're looking for. And to find the
bucket, well, we're going to use hash_string. We're going to
pass in the same word, the keyword, that's the
input key. The number of buckets is the
length of this table, so we're going to call hash_string,
passing in the key. And as the second input, we need
the length of the table, that's the number of buckets. So
that will get us a number which is the index of
the bucket we want. To get that bucket, we need to
use that as the index to select that element from htable,
and then we want to return the results. So that's all
we need to find the bucket. Let's look at that in
the Python interpreter. So here's the code we have so far.
We have the hash string procedure we defined that maps
the key word and a number of buckets to the position
where that should occur in the hash table. We have
the make_hashtable procedure that creates an empty table with that number
of buckets. And now we have the hashtable_get_bucket procedure that
takes a hash table and a key. And give this the
element of the hash table which is where that key would
belong using that hash string function to find the right position.
これが答えです
hashtable_get_bucketを定義します
2つの入力を使います
ハッシュテーブルをhtableと呼びましょう
そしてキーです これは探している単語です
バケットを見つけるためにhash_stringを使用します
同じ単語つまりキーワードを渡します
入力のキーです
バケット数はこのテーブルの要素数です
ではhash_stringを呼び出します キーを渡します
2つ目の入力としてテーブルの長さが必要です
これはバケット数です
これによって求めているバケットの
インデックスの数値がわかります
そのバケットを得るために
インデックスとして用いてhtableから
要素を選択する必要があります
結果を返します ここまでがバケットを
見つけるために必要なことのすべてです
Pythonインタプリタで見てみましょう
これがこれまでのコードです
定義したhash_string関数があります
これでキーワードとバケット数を
ハッシュテーブルでキーが現れる位置に対応づけます
make_hashtable関数です
そのバケット数で空のテーブルを作成します
hashtable_get_bucket関数があります
これはハッシュテーブルとキーを用います
そして適切な位置を見つけるために
そのhash_string関数を用いて
そのキーが属している
ハッシュテーブルの要素を与えます