Grokking Algorithms


Uses the length of the string as the index



Yüklə 348,95 Kb.
Pdf görüntüsü
səhifə114/122
tarix05.12.2023
ölçüsü348,95 Kb.
#173611
1   ...   110   111   112   113   114   115   116   117   ...   122
grokking-algorithms-illustrated-programmers-curious

Uses the length of the string as the index
 Answer: 
Consistent
Suppose you have these four hash functions that work with strings:
A. Return “1” for all input.
B. Use the length of the string as the index.
C. Use the first character of the string as the index. So, all strings 
starting with 
a
are hashed together, and so on.
D. Map every letter to a prime number: a = 2, b = 3, c = 5, d = 7,
e = 11, and so on. For a string, the hash function is the sum of 
all the characters modulo the size of the hash. For example, if 
your hash size is 10, and the string is “bag”, the index is 3 + 2 + 
17 % 10 = 22 % 10 = 2.
For each of the following examples, which hash functions would 
provide a good distribution? Assume a hash table size of 10 slots.
5.5
A phonebook where the keys are names and values are phone 
numbers. The names are as follows: Esther, Ben, Bob, and Dan.
Answer: 
Hash functions C and D would give a good distribution.
answers to exercises


228
5.6
A mapping from battery size to power. The sizes are A, AA, AAA, 
and AAAA.
Answer: 
Hash functions B and D would give a good distribution.
5.7
A mapping from book titles to authors. The titles are 
Maus

Fun 
Home
, and 
Watchmen
.
Answer:
Hash functions B, C, and D would give a good 
distribution.
CHAPTER 6
Run the breadth-first search algorithm on each of these graphs
to find the solution.
6.1
Find the length of the shortest path from start to finish.
Answer:
The shortest path has a length of 2.
6.2
Find the length of the shortest path from “cab” to “bat”.
Answer: 
The shortest path has a length of 2.

Yüklə 348,95 Kb.

Dostları ilə paylaş:
1   ...   110   111   112   113   114   115   116   117   ...   122




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin