CP164 Study Guide - Midterm Guide: Alex Proyas

39 views2 pages
14 Jun 2018
School
Course
Professor
The List / SortedList ADTs
Keys versus Values
The section above refers to keys and that the various lists may be searched for keys. A key is
simply an object in which only the subset of all possible attributes in that object have meaningful
values assigned. Those attributes must include the attributes used in the comparison operators.
In other words, a key is a value that has just enough information to allow us to search for the full
set of matching data in a data structure. A student ID number is sufficient for us to search a list
of students for a matching student. A movie title and year is sufficient for us to search a list of
movies for a matching movie that contains the rest of the information we require. Note that in
both these cases we still need our search attributes stored in an object of the correct type: i.e. a
Student object that holds only a student ID number, and a Movie object that contains only a title
and year. Because the data structures must be generic and work with objects of any type (albeit
any particular List must hold values of only one type), we cannot design our data structures to
search for only the string ID part of a Student object, or only the string title and integer year of a
Movie object. Thus our key values must still be the proper types of objects, just with some
attributes left empty. These 'extra' attributes are left empty because it would be pointless for us to
search for a movie if we already have all of the possible information it could have. Why search
for it, then?
The following is an example of how we can define a Movie object as a key to be searched for in
a List.
Define a new Movie object, assigning values to only the title and year attributes:
key_movie = Movie('Dark City', 1998, None, None, None)
The resulting movie object looks like this:
Title: Dark City
Year: 1998
Director: None
Rating: None
Genres: None
The important thing to recognize here is that this is sufficient information to use to find a
matching Movie object in a List. The Movie comparison functions use only the title and year
attributes to compare two movie objects. We can now use this to search for the full movie
information (assuming Dark City is actually stored in the list):
n = movie_list.index(key_movie)
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Document Summary

The section above refers to keys and that the various lists may be searched for keys. A key is simply an object in which only the subset of all possible attributes in that object have meaningful values assigned. Those attributes must include the attributes used in the comparison operators. In other words, a key is a value that has just enough information to allow us to search for the full set of matching data in a data structure. A student id number is sufficient for us to search a list of students for a matching student. A movie title and year is sufficient for us to search a list of movies for a matching movie that contains the rest of the information we require. Note that in both these cases we still need our search attributes stored in an object of the correct type: i. e. a.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers

Related Documents

Related Questions