ICS 33 Fall 2024
Exercise Set 3 Solutions
Problem 1
SELECT title
FROM employee
WHERE name = 'Constantin Phelps';
SELECT e.name
FROM employee AS e
INNER JOIN membership AS m ON m.employee_id = e.employee_id
INNER JOIN department AS d ON d.department_id = m.department_id
WHERE d.name = 'Sales';
SELECT e.name
FROM employee AS e
INNER JOIN employee_manager AS em ON em.employee_id = e.employee_id
INNER JOIN employee AS m ON m.employee_id = em.manager_id
WHERE m.name = 'Athina Muto';
Problem 2
def multiples(count, multiplier):
return [index * multiplier for index in range(1, count + 1)]
def with_non_zero_lengths(*keys):
return {k: len(k) for k in keys if len(k) != 0}
def make_diagonal(n):
return [['B' if row == column else None for row in range(n)] for column in range(n)]
Problem 3
There are at least a couple of reasons why keys in a dictionary must be hashable, mirroring similar rules for the elements of sets.
'Boo'
?" in a dictionary with millions of keys in it, we want that answer to be found in O(1) time (i.e., about as quickly as in a dictionary with ten keys); hashing offers an algorithm that meets that promise.The values, on the other hand, are not subject to the same requirement, because neither of those things is true of them.
Consequently, there's no particular reason to disallow the values from being unhashable (and, by extension, from being mutable). And, in fact, there's usefulness in having them be mutable; for example, we might have a dictionary in which the keys are people's names and the values are lists of other people who are their friends, with those lists changing over time.
Problem 4
An asymptotic analysis of dictionary comprehensions would revolve around understanding what they do behind the scenes, which boils down to this.
What are the associated costs of these operations?
O(1) + O(n) = O(n), so our closest-fit O-notation here is O(n).