DOC PREVIEW
Berkeley COMPSCI 61A - Lecture 13

This preview shows page 1-2-3-4-5-6-7-8-54-55-56-57-58-59-60-110-111-112-113-114-115-116-117 out of 117 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 117 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

61A Lecture 13Wednesday, September 28Tuesday, September 27, 2011Dictionaries{'Dem': 0}2Tuesday, September 27, 2011Limitations on Dictionaries3Tuesday, September 27, 2011Limitations on DictionariesDictionaries are unordered collections of key-value pairs.3Tuesday, September 27, 2011Limitations on DictionariesDictionaries are unordered collections of key-value pairs.Dictionaries do have two restrictions:3Tuesday, September 27, 2011Limitations on DictionariesDictionaries are unordered collections of key-value pairs.Dictionaries do have two restrictions:•A key of a dictionary cannot be an object of a mutable built-in type.3Tuesday, September 27, 2011Limitations on DictionariesDictionaries are unordered collections of key-value pairs.Dictionaries do have two restrictions:•A key of a dictionary cannot be an object of a mutable built-in type.•Two keys cannot be equal. There can be at most one value for a given key.3Tuesday, September 27, 2011Limitations on DictionariesDictionaries are unordered collections of key-value pairs.Dictionaries do have two restrictions:•A key of a dictionary cannot be an object of a mutable built-in type.•Two keys cannot be equal. There can be at most one value for a given key.This first restriction is tied to Python's underlying implementation of dictionaries.3Tuesday, September 27, 2011Limitations on DictionariesDictionaries are unordered collections of key-value pairs.Dictionaries do have two restrictions:•A key of a dictionary cannot be an object of a mutable built-in type.•Two keys cannot be equal. There can be at most one value for a given key.This first restriction is tied to Python's underlying implementation of dictionaries.The second restriction is an intentional consequence of the dictionary abstraction.3Tuesday, September 27, 2011Implementing Dictionaries4Tuesday, September 27, 2011Implementing Dictionaries4 def make_dict(): """Return a functional implementation of a dictionary."""Tuesday, September 27, 2011Implementing Dictionaries4 def make_dict(): """Return a functional implementation of a dictionary.""" records = []Tuesday, September 27, 2011Implementing Dictionaries4 def make_dict(): """Return a functional implementation of a dictionary.""" records = [] def getitem(key): for k, v in records: if k == key: return vTuesday, September 27, 2011Implementing Dictionaries4 def make_dict(): """Return a functional implementation of a dictionary.""" records = [] def getitem(key): for k, v in records: if k == key: return v def setitem(key, value):Tuesday, September 27, 2011Implementing Dictionaries4 def make_dict(): """Return a functional implementation of a dictionary.""" records = [] def getitem(key): for k, v in records: if k == key: return v def setitem(key, value): for item in records: if item[0] == key: item[1] = value returnTuesday, September 27, 2011Implementing Dictionaries4 def make_dict(): """Return a functional implementation of a dictionary.""" records = [] def getitem(key): for k, v in records: if k == key: return v def setitem(key, value): for item in records: if item[0] == key: item[1] = value return records.append([key, value])Tuesday, September 27, 2011Implementing Dictionaries4 def make_dict(): """Return a functional implementation of a dictionary.""" records = [] def getitem(key): for k, v in records: if k == key: return v def setitem(key, value): for item in records: if item[0] == key: item[1] = value return records.append([key, value]) def dispatch(message, key=None, value=None):Tuesday, September 27, 2011Implementing Dictionaries4 def make_dict(): """Return a functional implementation of a dictionary.""" records = [] def getitem(key): for k, v in records: if k == key: return v def setitem(key, value): for item in records: if item[0] == key: item[1] = value return records.append([key, value]) def dispatch(message, key=None, value=None): if message == 'getitem': return getitem(key)Tuesday, September 27, 2011Implementing Dictionaries4 def make_dict(): """Return a functional implementation of a dictionary.""" records = [] def getitem(key): for k, v in records: if k == key: return v def setitem(key, value): for item in records: if item[0] == key: item[1] = value return records.append([key, value]) def dispatch(message, key=None, value=None): if message == 'getitem': return getitem(key) elif message == 'setitem': setitem(key, value)Tuesday, September 27, 2011Implementing Dictionaries4 def make_dict(): """Return a functional implementation of a dictionary.""" records = [] def getitem(key): for k, v in records: if k == key: return v def setitem(key, value): for item in records: if item[0] == key: item[1] = value return records.append([key, value]) def dispatch(message, key=None, value=None): if message == 'getitem': return getitem(key) elif message == 'setitem': setitem(key, value) elif message == 'keys': return tuple(k for k, _ in records)Tuesday, September 27, 2011Implementing Dictionaries4 def make_dict(): """Return a functional implementation of a dictionary.""" records = [] def getitem(key): for k, v in records: if k == key: return v def setitem(key, value): for item in records: if item[0] == key: item[1] = value return records.append([key, value]) def dispatch(message, key=None, value=None): if message == 'getitem': return getitem(key) elif message == 'setitem': setitem(key, value) elif message == 'keys': return tuple(k for k, _ in records) elif message == 'values': return tuple(v for _, v in records)Tuesday, September 27, 2011Implementing Dictionaries4 def make_dict(): """Return a functional implementation of a dictionary.""" records = [] def getitem(key): for k, v in records: if k == key: return v def setitem(key, value): for item in records: if item[0] == key: item[1] = value return records.append([key, value]) def dispatch(message, key=None, value=None): if message == 'getitem': return getitem(key) elif message == 'setitem': setitem(key, value) elif message == 'keys': return tuple(k for k, _ in records) elif message == 'values': return tuple(v for _, v in records) return dispatchTuesday, September 27, 2011Implementing Dictionaries4 def


View Full Document

Berkeley COMPSCI 61A - Lecture 13

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lab 4

Lab 4

4 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more
Download Lecture 13
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 13 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 13 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?