12 Containers 4pp
12 Containers 4pp
You can convert while/for statements to recursion without inventing new logic:
• Each pass through the body of a while/for statement is replaced by a recursive call.
• Instead of using assignment statements, assign names to values using a call expression.
• If needed, introduce a new function with an argument for every value that must be tracked.
Iteration and Recursion
(Demo)
4
The Closure Property of Data Types
• A method for combining data values satisfies the closure property if:
The result of combination can itself be combined using the same method
Box-and-Pointer Notation • Hierarchical structures are made up of parts, which themselves are made up
of parts, and so on
Lists are represented as a row of index-labeled adjacent boxes, one per element Lists are represented as a row of index-labeled adjacent boxes, one per element
Each box either contains a primitive value or points to a compound value Each box either contains a primitive value or points to a compound value
7 8
pythontutor.com/composingprograms.html#code=pair%20%3D%20[1,%202]%0A%0Anested_list%20%3D%20[[1,%202],%20[],%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20[[3,%20False,%20None],
%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20[4,%20lambda%3A%205]]]&mode=display&origin=composingprograms.js&cumulative=true&py=3&rawInputLstJSON=[]&curInstr=4
Slicing Creates New Values
10
pythontutor.com/composingprograms.html#code=digits%20%3D%20[1,%208,%202,%208]%0Astart%20%3D%20digits[%3A1]%0Amiddle%20%3D%20digits[1%3A3]%0Aend%20%3D%20digits[2%3A]%0Afull%20%3D%20digits[%3A]&cumulative%3Dtrue&curInstr%3D5&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=[]
Aggregation
Several built-in functions take iterable arguments and aggregate them into a value
12
Discussion Question
Strings
2 ** n for n in range(100) lambda x: abs(1000 - x)
min( [ ___________________________ ], key= _________________________ )
13
(Demo)
15 16
Limitations on Dictionaries
{'Dem': 0} If you want to associate multiple values with a key, store them all in a sequence value
18
{<key exp>: <value exp> for <name> in <iter exp> if <filter exp>} Implement index, which takes a sequence of keys, a sequence of values, and a two-argument
match function. It returns a dictionary from keys to lists in which the list for a key k
contains all values v for which match(k, v) is a true value.
Short version: {<key exp>: <value exp> for <name> in <iter exp>}
2. Create an empty result dictionary that is the value of the expression >>> index([7, 9, 11], range(30, 50), lambda k, v: v % k == 0)
{7: [35, 42, 49], 9: [36, 45], 11: [33, 44]}
3. For each element in the iterable value of <iter exp>: """
{k: [v for v in values if match(k, v)] for k in keys}
A. Bind <name> to that element in the new frame from step 1 return __________________________________________________________
B. If <filter exp> evaluates to a true value, then add to the result dictionary
an entry that pairs the value of <key exp> to the value of <value exp>
19 20