= Solution
Select entries having the requested column. The <representation invariant> ensures that the selected <list> has length at most one.
``
def get(i: Int, j: Int): Double = {
require(0 <= i && i < N && 0 <= j && j < N)
val entries = rows(i).filter(e => e._1 == j)
if (entries.isEmpty) 0.0 else entries.head._2
}
``
Consequently
$$
\boxed{\mathrm{get}(i,j)=A_{ij}.}
$$
This version follows the hint and has <time complexity> linear in the number of stored entries in row `i`. An ordered early-exit scan is also possible.
Back to article page