5
5 R EVIEW : O RDERS OF G ROWTH Θ(n) We know that the result of n % 3 is 0, 1, or 2 (the base case), so we know that the first recursive call will always result in a base case and we can treat it as a constant time operation. The second recursive call will take (about) n recursive calls before reaching a base case (we subtract one from n each time). So we have Θ(n) recursive calls with constant amount of work done for each call.

6
6 D ATA A BSTRACTION We want to be able to think about data in terms of its meaning rather than in terms of the way it is represented. Data abstraction allows us to isolate: – How the data is represented (as parts) – How the data is manipulated (as units) We do this by using functions to help create a division between these two cases.

11
11 How would I write a function to invert (flip) a rational number using the constructor and selectors we are using for rational numbers? P RACTICE : U SING A BSTRACTIONS

12
12 How would I write a function to invert (flip) a rational number using the constructor and selectors we are using for rational numbers? def invert_rat(r): return make_rat(denom(r), numer(r)) P RACTICE : U SING A BSTRACTIONS

14
14 T UPLES : O UR F IRST D ATA S TRUCTURE The Python data type tuple is an example of what we call a data structure in computer science. A data structure is a type of data that exists primarily to hold other pieces of data in a specific way.

16
16 Write the higher order function map, which takes a function, fn, and a tuple of values, vals, and returns a the tuple of results of applying fn to each value in vals. def map(fn, vals): results = () for v in vals: results = results + (fn(v),) return results P RACTICE : U SING T UPLES AND A BSTRACTIONS

17
17 A NNOUNCEMENTS Project 1 autograder is running now. Next week, we will move to 105 Stanley for the rest of the summer. Midterm 1 is on July 9. – We will have a review session closer to the date. If you need accommodations for the midterm, please notify DSP by the end of this week. HW1 grade should be available on glookup.

22
22 P RACTICE : D ATA A BSTRACTION Suppose that Louis Reasoner wrote the following function prod_rats that takes a tuple of rational numbers using our ADT and returns their product. Correct his code so that he does not have any data abstraction violations. def prod_rats(rats): total, i = (1, 1), 0 while i < len(rats): total = (total[0] * rats[i][0], total[1] * rats[i][1]) i += 1 return total

23
23 P RACTICE : D ATA A BSTRACTION Suppose that Louis Reasoner wrote the following function prod_rats which takes a tuple of rational numbers using our ADT and returns their product. Correct his code so that he does not have any data abstraction violations. def prod_rats(rats): total, i = make_rat(1, 1), 0 while i < len(rats): total = make_rat(numer(total) * numer(rats[i]), denom(total) * denom(rats[i])) i += 1 return total

24
24 P RACTICE : D ATA A BSTRACTION Say I wrote the following functions to define my student ADT. def make_student(name, id): return (name, id) def student_name(s): return s[0] def student_id(s): return s[1] If I changed the student ADT to also include the student’s age, what functions would I have to add or change in order to complete the abstraction?

25
25 P RACTICE : D ATA A BSTRACTION Say I wrote the following functions to define my student ADT. def make_student(name, id): return (name, id) def student_name(s): return s[0] def student_id(s): return s[1] If I changed the student ADT to also include the student’s age, what functions would I have to add or change in order to complete the abstraction? You would have to change make_student to take this new parameter. If you just represent a student as the tuple (name, id, age), then you only have to add a selector for the student’s age. The other two selectors would not have to be modified in this case.

26
26 C ONCLUSION Tuples are a nice way to group data in Python. Learned how to design new types of data by using data abstraction. Preview: Useful data structures.

27
27 E XTRAS : U SING F UNCTIONS TO C REATE ADT S It turns out you don’t need to have something like tuples in a language in order to group data together. Say I wanted to make a pair abstraction, which is like a tuple of length 2. I could do this with just functions: def make_pair(first, second): def pair(msg): if msg == “first”: return first elif msg == “second”: return second return pair def first(p): return p(“first”) def second(p): return p(“second”)

28
28 E XTRAS : M ORE ABOUT S TRINGS Strings and tuples are both sequences, meaning that they are things that you can iterate over with a for loop. Interestingly, they can also be indexed into and sliced like tuples. >>> for letter in “abc”:... print(letter) a b c >>> “asdf”[2] d >>> “slaughterhouse”[1:9] “laughter”