Exercise

  1. 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

  1. HashMap and dict.
  • 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 for dict in Python?

  1. 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.)

  1. Please write unit tests for naive_two_sum.py. (Hint: you can refer to the Java implementation NaiveTwoSumTest.java.)

  1. Please give a big O characterization in terms of n for each function shown in example.py.

  1. Perform experimental analysis to test the hypothesis that Java's sort() or Python's sorted() 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)

  1. Please give a big O characterization in terms of n for BinarySearch.java or binary_search.py.

  1. Please design experiments to compare the efficiency between fast_two_sum.py and naive_two_sum.py.