Lab 4 - Lists, and Data Abstraction - CS 61A Summer 2019 PDF
Lab 4 - Lists, and Data Abstraction - CS 61A Summer 2019 PDF
Starter Files
Download lab04.zip (lab04.zip). Inside the archive, you will nd starter les for the
questions in this lab, along with a copy of the Ok (ok) autograder.
Submission
By the end of this lab, you should have submitted the lab with python3 ok --submit . You
may submit more than once before the deadline; only the nal submission will be graded.
Check that you have successfully submitted your code on okpy.org (https://okpy.org/).
Topics
Consult this section if you need a refresher on the material for this lab. It's okay to skip
directly to the questions and refer back here should you get stuck.
List Introduction
List Comprehensions
Data Abstraction
Required Questions
https://cs61a.org/lab/lab04/ 1/10
2019/8/13 Lab 4: Lists, and Data Abstraction | CS 61A Summer 2019
Lists Practice
Q1: List Indexing
Use Ok to test your knowledge with the following "List Indexing" questions:
python3 ok -q indexing -u
For each of the following lists, what is the list indexing expression that evaluates to 7 ? For
example, if x = [7] , then the answer would be x[0] . You can use the interpreter or Python
Tutor to experiment with your answers.
Toggle Solution
What would Python display? If you get stuck, try it out in the Python interpreter!
>>> lst[3][0]
84
Toggle Solution
Q2: Couple
Implement the function couple , which takes in two lists and returns a list that contains
lists with i-th elements of two sequences coupled together. You can assume the lengths of
two sequences are the same. Try using a list comprehension.
https://cs61a.org/lab/lab04/ 2/10
2019/8/13 Lab 4: Lists, and Data Abstraction | CS 61A Summer 2019
Toggle Solution
python3 ok -q couple
Q3: Enumerate
Implement enumerate , which pairs the elements of a sequence with their indices, offset by
a starting value. enumerate takes a sequence s and a starting value start . It returns a list
of pairs, in whichthe i-th element is i + start paired with the i-th element of s . For
example:
https://cs61a.org/lab/lab04/ 3/10
2019/8/13 Lab 4: Lists, and Data Abstraction | CS 61A Summer 2019
Toggle Solution
python3 ok -q enumerate
make_city(name, lat, lon) : Creates a city object with the given name, latitude, and
longitude.
We also have the following selectors in order to get the information for each city:
Here is how we would use the constructor and selectors to create cities and extract their
information:
All of the selector and constructor functions can be found in the lab le, if you are curious
to see how they are implemented. However, the point of data abstraction is that we do not
need to know how an abstract data type is implemented, but rather just how we can
interact with and use the data type.
Q4: Distance
We will now implement the function distance , which computes the distance between two
city objects. Recall that the distance between two coordinate pairs (x1, y1) and (x2, y2)
can be found by calculating the sqrt of (x1 - x2)**2 + (y1 - y2)**2 . We have already
imported sqrt for your convenience. Use the latitude and longitude of a city as its
coordinates; you'll need to use the selectors to access this info!
https://cs61a.org/lab/lab04/ 4/10
2019/8/13 Lab 4: Lists, and Data Abstraction | CS 61A Summer 2019
Toggle Solution
python3 ok -q distance
You may only use the selectors and constructors introduced above and the distance
function you just de ned for this question.
Hint: How can use your distance function to nd the distance between the given
location and each of the given cities?
https://cs61a.org/lab/lab04/ 5/10
2019/8/13 Lab 4: Lists, and Data Abstraction | CS 61A Summer 2019
Toggle Solution
python3 ok -q closer_city
When writing functions that use an ADT, we should use the constructor(s) and selector(s)
whenever possible instead of assuming the ADT's implementation. Relying on a data
abstraction's underlying implementation is known as violating the abstraction barrier, and
we never want to do this!
It's possible that you passed the doctests for distance and closer_city even if you violated
the abstraction barrier. To check whether or not you did so, run the following command:
python3 ok -q check_abstraction
https://cs61a.org/lab/lab04/ 6/10
2019/8/13 Lab 4: Lists, and Data Abstraction | CS 61A Summer 2019
The make_check_abstraction function exists only for the doctest, which swaps out the
implementations of the city abstraction with something else, runs the tests from the
previous two parts, then restores the original abstraction.
The nature of the abstraction barrier guarantees that changing the implementation of an
ADT shouldn't affect the functionality of any programs that use that ADT, as long as the
constructors and selectors were used properly.
If you passed the Ok tests for the previous questions but not this one, the x is simple!
Just replace any code that violates the abstraction barrier, i.e. creating a city with a new list
object or indexing into a city, with the appropriate constructor or selector.
Make sure that your functions pass the tests with both the rst and the second
implementations of the City ADT and that you understand why they should work for both
before moving on.
Optional Question
This question can be found in lab04_extra.py .
>>> round(10.5)
10
>>> round(10.51)
11
def squares(s):
"""Returns a new list containing square roots of the elements of the
original list that are perfect squares.
https://cs61a.org/lab/lab04/ 7/10
2019/8/13 Lab 4: Lists, and Data Abstraction | CS 61A Summer 2019
This is great, but it requires that we have an is_perfect_square function. How might we
check if something is a perfect square?
If the square root of a number is a whole number, then it is a perfect square. For
example, sqrt(61) = 7.81024... (not a perfect square) and sqrt(49) = 7 (perfect
square).
Once we obtain the square root of the number, we just need to check if something is a
whole number. The is_perfect_square function might look like:
def is_perfect_square(x):
return is_whole(sqrt(x))
One last piece of the puzzle: to check if a number is whole, we just need to see if it
has a decimal or not. The way we've chosen to do it in the solution is to compare the
original number to the round version (thus removing all decimals), but a technique
employing oor division ( // ) or something else entirely could work too.
We've written all these helper functions to solve this problem, but they are actually all very
short. Therefore, we can just copy the body of each into the original list comprehension,
arriving at the solution we nally present.
Toggle Solution
python3 ok -q squares
Implement key_of_min_value , which takes in a dictionary d and returns the key that
corresponds to the minimum value in d . This behavior differs from just calling min on a
dictionary, which would return the smallest key. Make sure your solution is only one line
and uses the min function.
https://cs61a.org/lab/lab04/ 8/10
2019/8/13 Lab 4: Lists, and Data Abstraction | CS 61A Summer 2019
def key_of_min_value(d):
"""Returns the key in a dict d that corresponds to the minimum value of d.
>>> letters = {'a': 6, 'b': 5, 'c': 4, 'd': 5}
>>> min(letters)
'a'
>>> key_of_min_value(letters)
'c'
"""
return min(d, key=lambda x: d[x])
Toggle Solution
python3 ok -q key_of_min_value
https://cs61a.org/lab/lab04/ 9/10
2019/8/13 Lab 4: Lists, and Data Abstraction | CS 61A Summer 2019
CS 61A (/)
Weekly Schedule (/weekly.html)
Staff (/staff.html)
Resources (/resources.html)
Studying Guide (/articles/studying.html)
Policies (/articles/about.html)
Assignments (/articles/about.html#assignments)
Exams (/articles/about.html#exams)
Grading (/articles/about.html#grading)
https://cs61a.org/lab/lab04/ 10/10