---
title: Problem set 4
heading: "Problem set 4: collections"
eyebrow: A4 · Problem set 4
type: problems
---

::: hook
These labs use lessons 12 to 16: lists, dictionaries and sets, iteration patterns, and errors and testing. They're the everyday chores of analyzing measurements and handling parts lists.
:::

::: pool
pick: 5

::: check
kind: python
given: k = 0..2 int

## Each string in the starter's list is like `"R1=4.7k"`. Using the `parse` function given, set `bom` to a dictionary from each name to its value in base units.

```python starter
MULT = {"p": 1e-12, "n": 1e-9, "u": 1e-6, "m": 1e-3, "k": 1e3, "M": 1e6}

def parse(text):
    if text[-1] in MULT:
        return float(text[:-1]) * MULT[text[-1]]
    return float(text)

lines = [["R1=4.7k", "R2=10k", "C1=100n"], ["L1=10u", "C2=1u", "R3=330"], ["R4=2.2M", "C3=47p"]][k]
bom = {}
print(bom)
```

```python solution
MULT = {"p": 1e-12, "n": 1e-9, "u": 1e-6, "m": 1e-3, "k": 1e3, "M": 1e6}

def parse(text):
    if text[-1] in MULT:
        return float(text[:-1]) * MULT[text[-1]]
    return float(text)

lines = [["R1=4.7k", "R2=10k", "C1=100n"], ["L1=10u", "C2=1u", "R3=330"], ["R4=2.2M", "C3=47p"]][k]
bom = {}
for line in lines:
    name, text = line.split("=")
    bom[name] = parse(text)
print(bom)
```

```python tests
import math
want = [{"R1": 4700, "R2": 1e4, "C1": 1e-7}, {"L1": 1e-5, "C2": 1e-6, "R3": 330}, {"R4": 2.2e6, "C3": 4.7e-11}][k]
assert set(bom) == set(want), f"bom should have the keys {sorted(want)}. Split each line at '='."
assert all(math.isclose(bom[n], want[n]) for n in want), "Some values are off. Pass the text after '=' to parse."
```
:::

::: check
kind: python
given: k = 0..3 int

## Set `m` and `s` to the mean and sample standard deviation of `x`, then `outliers` to the readings more than 2 standard deviations from the mean, in their original order.

```python starter
x = [[5.0, 5.1, 4.9, 5.0, 6.2, 5.05, 4.95, 5.0], [3.3, 3.31, 3.29, 2.5, 3.3, 3.32],
     [10, 10.2, 9.8, 10.1, 9.9, 13.0, 10.0, 7.0], [1.0, 1.01, 0.99, 1.0, 1.02, 0.98]][k]
m = s = 0
outliers = []
print(m, s, outliers)
```

```python solution
import statistics
x = [[5.0, 5.1, 4.9, 5.0, 6.2, 5.05, 4.95, 5.0], [3.3, 3.31, 3.29, 2.5, 3.3, 3.32],
     [10, 10.2, 9.8, 10.1, 9.9, 13.0, 10.0, 7.0], [1.0, 1.01, 0.99, 1.0, 1.02, 0.98]][k]
m = statistics.mean(x)
s = statistics.stdev(x)
outliers = [v for v in x if abs(v - m) > 2 * s]
print(m, s, outliers)
```

```python tests
import statistics
xs = [[5.0, 5.1, 4.9, 5.0, 6.2, 5.05, 4.95, 5.0], [3.3, 3.31, 3.29, 2.5, 3.3, 3.32],
      [10, 10.2, 9.8, 10.1, 9.9, 13.0, 10.0, 7.0], [1.0, 1.01, 0.99, 1.0, 1.02, 0.98]][k]
mm, ss = statistics.mean(xs), statistics.stdev(xs)
assert abs(m - mm) < 1e-12 and abs(s - ss) < 1e-12, "m and s should be the mean and the sample standard deviation."
assert outliers == [v for v in xs if abs(v - mm) > 2 * ss], "outliers should be the readings with |v − m| > 2s, in order."
```
:::

::: check
kind: python
given: k = 0..2 int

## Count how many parts connect to each node. Set `degree` to a dictionary from node name to that count.

```python starter
netlist = [[("R1", "in", "a"), ("R2", "a", "0"), ("C1", "a", "0"), ("R3", "in", "0")],
           [("V1", "vcc", "0"), ("R1", "vcc", "b"), ("Q1", "b", "0"), ("R2", "vcc", "b")],
           [("L1", "x", "y"), ("C1", "y", "0"), ("R1", "y", "0"), ("C2", "x", "0")]][k]
degree = {}
print(degree)
```

```python solution
netlist = [[("R1", "in", "a"), ("R2", "a", "0"), ("C1", "a", "0"), ("R3", "in", "0")],
           [("V1", "vcc", "0"), ("R1", "vcc", "b"), ("Q1", "b", "0"), ("R2", "vcc", "b")],
           [("L1", "x", "y"), ("C1", "y", "0"), ("R1", "y", "0"), ("C2", "x", "0")]][k]
degree = {}
for name, a, b in netlist:
    degree[a] = degree.get(a, 0) + 1
    degree[b] = degree.get(b, 0) + 1
print(degree)
```

```python tests
want = [{"in": 2, "a": 3, "0": 3}, {"vcc": 3, "0": 2, "b": 3}, {"x": 2, "y": 3, "0": 3}][k]
assert degree == want, f"degree should be {want}. Count both ends of every part."
```
:::

::: check
kind: python
given: k = 0..2 int

## For the sweep in the starter, set `gain_db` to the list of gains in dB ($20\log_{10}$), and `f3` to the first frequency whose gain is more than 3 dB below the first one's.

```python starter
import math
f = [10, 100, 1000, 2000, 5000, 10000, 20000]
g = [[1.0, 1.0, 0.99, 0.95, 0.70, 0.45, 0.24], [2.0, 2.0, 1.9, 1.7, 1.2, 0.8, 0.4],
     [0.5, 0.5, 0.5, 0.49, 0.47, 0.33, 0.2]][k]
gain_db = []
f3 = None
print(gain_db, f3)
```

```python solution
import math
f = [10, 100, 1000, 2000, 5000, 10000, 20000]
g = [[1.0, 1.0, 0.99, 0.95, 0.70, 0.45, 0.24], [2.0, 2.0, 1.9, 1.7, 1.2, 0.8, 0.4],
     [0.5, 0.5, 0.5, 0.49, 0.47, 0.33, 0.2]][k]
gain_db = [20 * math.log10(v) for v in g]
f3 = None
for freq, d in zip(f, gain_db):
    if d < gain_db[0] - 3:
        f3 = freq
        break
print(gain_db, f3)
```

```python tests
import math
gs = [[1.0, 1.0, 0.99, 0.95, 0.70, 0.45, 0.24], [2.0, 2.0, 1.9, 1.7, 1.2, 0.8, 0.4], [0.5, 0.5, 0.5, 0.49, 0.47, 0.33, 0.2]][k]
assert len(gain_db) == 7 and all(abs(a - 20 * math.log10(b)) < 1e-12 for a, b in zip(gain_db, gs)), "gain_db should be 20·log10 of each gain."
assert f3 == [5000, 5000, 10000][k], "f3 should be the first frequency more than 3 dB down from the first gain."
```
:::

::: check
kind: python
given: V = 5 | 12; k = 0..2 int

## Each part is a tuple `(name, ohms)`. Set `ranked` to the list of names sorted by the power each dissipates at `V` = {V} V, largest first.

```python starter
parts = [[("R1", 100), ("R2", 470), ("R3", 47), ("R4", 1000)], [("RA", 2200), ("RB", 220), ("RC", 22)],
         [("R5", 330), ("R6", 3300), ("R7", 33), ("R8", 68)]][k]
ranked = []
print(ranked)
```

```python solution
parts = [[("R1", 100), ("R2", 470), ("R3", 47), ("R4", 1000)], [("RA", 2200), ("RB", 220), ("RC", 22)],
         [("R5", 330), ("R6", 3300), ("R7", 33), ("R8", 68)]][k]
ranked = [name for name, r in sorted(parts, key=lambda p: V**2 / p[1], reverse=True)]
print(ranked)
```

```python tests
want = [["R3", "R1", "R2", "R4"], ["RC", "RB", "RA"], ["R7", "R8", "R5", "R6"]][k]
assert ranked == want, f"ranked should be {want}: the smallest resistance dissipates the most at a fixed voltage."
```
:::

::: check
kind: python
given: w = 3 | 4 | 5; k = 0..2 int

## Set `avg` to the moving average of `x` over a window of `w` = {w}: entry `i` is the mean of `x[i:i+w]`, for every full window.

```python starter
x = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [5, 3, 8, 1, 9, 2, 7, 4], [0, 0, 10, 0, 0, 10, 0, 0, 10]][k]
avg = []
print(avg)
```

```python solution
x = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [5, 3, 8, 1, 9, 2, 7, 4], [0, 0, 10, 0, 0, 10, 0, 0, 10]][k]
avg = [sum(x[i:i + w]) / w for i in range(len(x) - w + 1)]
print(avg)
```

```python tests
xs = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [5, 3, 8, 1, 9, 2, 7, 4], [0, 0, 10, 0, 0, 10, 0, 0, 10]][k]
want = [sum(xs[i:i + w]) / w for i in range(len(xs) - w + 1)]
assert len(avg) == len(want), f"There are {len(want)} full windows: len(x) − w + 1."
assert all(abs(a - b) < 1e-12 for a, b in zip(avg, want)), "Each entry is the mean of x[i:i+w]."
```
:::

::: check
kind: python
given: k = 0..2 int

## Some strings in the starter's list aren't numbers. Set `total` to the sum of the ones that are, and `bad` to how many weren't, using `try` and `except ValueError`.

```python starter
texts = [["3.3", "5", "n/a", "12", "", "1e3"], ["abc", "2", "2.5", "7x"], ["-1", "1", "0.5", "--"]][k]
total = 0
bad = 0
print(total, bad)
```

```python solution
texts = [["3.3", "5", "n/a", "12", "", "1e3"], ["abc", "2", "2.5", "7x"], ["-1", "1", "0.5", "--"]][k]
total = 0
bad = 0
for t in texts:
    try:
        total += float(t)
    except ValueError:
        bad += 1
print(total, bad)
```

```python tests
want_total, want_bad = [(1020.3, 2), (4.5, 2), (0.5, 1)][k]
assert abs(total - want_total) < 1e-9, f"total should be {want_total}."
assert bad == want_bad, f"bad should be {want_bad}."
```
:::
:::
