HW 7 - Streams, SQL
Instructions: https://inst.eecs.berkeley.edu/~cs61a/su19/hw/hw07/
Solutions:
Streams
Q1: Multiples of 3
Define implicitly an infinite stream multiples-of-three
that contains the multiples of 3.
You may use the map-stream
function defined below. map-stream
takes in a one-argument function f
and a stream s
and returns a new stream containing the elements of s
with f
applied.
Do not define any other helper functions.
Q1: Solution
Q2: Run-Length Encoding
Run-length encoding is a very simple data compression technique, whereby runs of data are compressed and stored as a single value. A run is defined to be a contiguous sequence of the same number. For example, in the (finite) sequence
there are four runs: one each of 1, 6, 2, and 5. We can represent the same sequence as a sequence of two-element lists:
We will extend this idea to streams. Write a function called rle
that takes in a stream of data, and returns a corresponding stream of two-element lists, which represents the run-length encoded version of the stream. You do not have to consider compressing infinite streams - the stream passed in will eventually terminate with nil
.
Q2: Solution
SQL
Q3: Size of Dogs
The Fédération Cynologique Internationale classifies a standard poodle as over 45 cm and up to 60 cm. The sizes
table describes this and other such classifications, where a dog must be over the min
and less than or equal to the max
in height to qualify as a size
.
Create a size_of_dogs
table with two columns, one for each dog's name
and another for its size
.
Q3: Solution
Q4: By Parent Height
Create a table by_parent_height
that has a column of the names of all dogs that have a parent
, ordered by the height of the parent from tallest parent to shortest parent.
Q4: Solution
Q5: Sentences
There are two pairs of siblings that have the same size. Create a table that contains a row with a string for each of these pairs. Each string should be a sentence describing the siblings by their size.
Q5: Solution
Q6: Low Variance
We want to create a table which contains the total height of all dogs that share a fur type, but only for the fur types where the heights are not spread out too much. To approximate this, we'll only consider the fur types where each dog with that fur is within 30% of the average height of all dogs with that fur.
For example, if the average height for short-haired dogs is 10, then in order to be included in our output, all dogs with short hair must have a height of at most 13 and at least 7.
Your output should first include the fur type and then the total height for the fur types that meet this criteria
Q6: Solution
Last updated