Professional Documents
Culture Documents
7 Dictionaries
Do Now Questions
• A hash function calculates hash values as follows:
• each character in the key is converted to its ASCII decimal numeric code (i.e. A
will be converted to 65,65)
• the numeric codes are summed to give a total
• the total is divided by the number of slots in the hash table using modular
division
• the result is the hash value
1. Given an alphanumeric Key of “CSAA”, and 55 being the number of
slots, calculate the has value.
2. What is rehashing
3. Given this hashing function below, and a number_of_slots of 48
Calculate the hash values of:
a) 31971
b) 43219
c) 56342
d) ,96756
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries
Answers
1. 23
2. Over time, items will be added and deleted from the hash table. If
the hash table starts to fill up, or a large number of items are in
the wrong place, the performance of the hash table will degrade.
Rehashing can be used to address this problem.
3.
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries
Dictionaries
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries
Objectives
• Know how a dictionary works and how it can be used
• Be aware of simple applications of dictionary in Python
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries
Dictionaries
Example
• Each record has a key – Customer ID
• Key links all the data that is stored about the customer
• We can retrieve/add/delete a customer record at any point
• Often used to implement databases
• Example:
Key e.g. CustID Associated Data
01234 James Cochran, 12 Harbour Mews, Leicester
If we search for 01235 in
01235 Mary Abbot, 56 Eagle Street, Manchester
the dictionary, Mary
01236 Keith Fletcher, 3 Yarborough Road, Leeds Abbot’s details would be
01237 David Smith, 68 Lemon Street, Derby returned.
01238 Tim Evans, 87 Forrest Drive, Manchester
{01234: “James Conchran, ….”, 01235: “Mary Abbot, ….”, 01236: “Keith Flethcher, …”, ……}
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries
Pseudocode Python
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries
Pseudocode Python
Use the key to retrieve associated data. This will return the associated data
value.
The key is used in the same way that an index is used for an array except we use
the key rather than an index.
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries
Update a value
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries
Remove a value
Pseudocode Python
If you remove a key, its associated value will be removed as well. If the key
does not exist in the dictionary, an exception will be thrown.
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries
Dictionaries in Python
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries
Python Task
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB
4.2.7 Dictionaries
Tasks - Dictionaries
1. Update A3 sheet
Spec Requirements:
1. Be familiar with the concept of a dictionary – collection of
key/value pairs
2. Simple applications of dictionaries e.g. information retrieval
3. Have experience using a dictionary data structure in a
programming language
Keywords
Objectives Know how a dictionary works and how it can be used Key, value, retrieve
Be aware of simple applications of dictionary in VB