Exercise
- In CoffeeDB.java, we maintain a collection of coffees in an
ArrayList
, and this class offers a simple search method:
public List<Coffee> findByName(String name) {
...
}
Your task is to complete its Python version below in coffee_db.py:
def find_by_name(self, name):
pass
HashMap
anddict
.
- What would you get if the key does not exist for
HashMap
in Java? - What is the difference between indexing syntax and
get()
to obtain a value fordict
in Python?
- Please add a method to delete books by the names from a shop cart in ShopCart.java or shop_cart.py. (Hint: it does not make sense to store an item when its amount is less or equal 0.)
- Please write unit tests for naive_two_sum.py. (Hint: you can refer to the Java implementation NaiveTwoSumTest.java.)
- Please give a big O characterization in terms of
n
for each function shown in example.py.
- Perform experimental analysis to test the hypothesis that Java's
sort()
or Python'ssorted()
method runs in \(O(n\log{n})\) on average.
// a is a random list
List<Integer> a = Arrays.asList(1, 9, 4, 6);
Collections.sort(a);
# a is a random list
a = [1, 9, 6, 4]
a = sorted(a)
- Please give a big O characterization in terms of
n
for BinarySearch.java or binary_search.py.
- Please design experiments to compare the efficiency between fast_two_sum.py and naive_two_sum.py.