---
title: Problem set 2
heading: "Problem set 2: the basics"
eyebrow: A4 · Problem set 2
type: problems
---

::: hook
These labs use lessons 1 to 10: numbers, strings, conditionals, loops and functions. Each one is a small, complete engineering calculation. Read the hidden tests' messages when a check fails: they say which case went wrong.
:::

::: pool
pick: 5

::: check
kind: python
given: Q = 220 | 1000 | 3000; I = 0.5 | 2 | 15

## Write `battery_life(capacity_mAh, current_mA, derate=0.8)` returning the hours a battery lasts when only `derate` of its rated capacity is usable. Set `h1 = battery_life(Q, I)` and `h2 = battery_life(Q, I, derate=1.0)` for `Q` = {Q} mAh and `I` = {I} mA.

```python starter
def battery_life(capacity_mAh, current_mA, derate=0.8):
    return 0

h1 = battery_life(Q, I)
h2 = battery_life(Q, I, derate=1.0)
print(h1, h2)
```

```python solution
def battery_life(capacity_mAh, current_mA, derate=0.8):
    return capacity_mAh * derate / current_mA

h1 = battery_life(Q, I)
h2 = battery_life(Q, I, derate=1.0)
print(h1, h2)
```

```python tests
import math
assert math.isclose(battery_life(1000, 10), 80), "battery_life(1000, 10) should be 80 h: 80% of 1000 mAh at 10 mA."
assert math.isclose(battery_life(1000, 10, derate=0.5), 50), "The derate argument should scale the usable capacity."
assert math.isclose(h1, Q * 0.8 / I) and math.isclose(h2, Q / I), "h1 and h2 don't match for this lab's values."
```
:::

::: check
kind: python
given: v = 0.3 | 0.8 | 1.4 | 2.0 | 3.1

## TTL logic reads an input as low at or below 0.8 V, high at or above 2.0 V, and undefined in between. Write `level(v)` returning `"low"`, `"high"` or `"undefined"`, and set `s = level(v)` for `v` = {v} V.

```python starter
def level(v):
    if v < 1.4:
        return "low"
    return "high"

s = level(v)
print(s)
```

```python solution
def level(v):
    if v <= 0.8:
        return "low"
    if v >= 2.0:
        return "high"
    return "undefined"

s = level(v)
print(s)
```

```python tests
assert level(0.8) == "low" and level(2.0) == "high", "The thresholds are inclusive: 0.8 V is low and 2.0 V is high."
assert level(1.0) == "undefined" and level(1.9) == "undefined", "Between 0.8 and 2.0 V the level is undefined."
assert level(0) == "low" and level(5) == "high", "Check the extremes."
```
:::

::: check
kind: python
given: tol = 1e-6 | 1e-8 | 1e-10

## Add up $\sum 1/k^2$ for $k = 1, 2, 3, \ldots$, stopping before the first term smaller than `tol` = {tol}. Set `total` to the sum and `n` to how many terms you added.

```python starter
total = 0
n = 0
print(total, n)
```

```python solution
total = 0.0
n = 0
k = 1
while 1 / k**2 >= tol:
    total += 1 / k**2
    n += 1
    k += 1
print(total, n)
```

```python tests
import math
assert 1 / n**2 >= tol and 1 / (n + 1)**2 < tol, "Add every term that's at least tol, and stop at the first one below it."
assert abs(total - sum(1 / k**2 for k in range(1, n + 1))) < 1e-12, "total should be the sum of the terms you added."
assert abs(total - math.pi**2 / 6) < 2 / n, "The sum should be close to π²/6."
```
:::

::: check
kind: python
given: Vs = 3.3 | 5 | 12; R = 220 | 1000 | 4700

## A diode and a resistor `R` = {R} Ω are in series across `Vs` = {Vs} V. The diode's voltage is $V_d = 0.025\ln(I / 10^{-14})$. Starting from $I = (V_s - 0.7)/R$, repeat $I \leftarrow (V_s - V_d(I))/R$ until $I$ changes by less than $10^{-15}$ A. Set `I` to the result and `Vd` to the diode's voltage then.

```python starter
import math
I = (Vs - 0.7) / R
Vd = 0.7
print(I, Vd)
```

```python solution
import math
I = (Vs - 0.7) / R
while True:
    Inew = (Vs - 0.025 * math.log(I / 1e-14)) / R
    if abs(Inew - I) < 1e-15:
        I = Inew
        break
    I = Inew
Vd = 0.025 * math.log(I / 1e-14)
print(I, Vd)
```

```python tests
import math
vd = 0.025 * math.log(I / 1e-14)
assert abs(Vd - vd) < 1e-12, "Vd should be 0.025·ln(I/1e-14) at your final I."
assert abs(I - (Vs - vd) / R) < 1e-13, "At the answer, I = (Vs − Vd)/R should hold. Keep iterating until the change is below 1e-15."
```
:::

::: check
kind: python
given: n = 5..255 int

## Write `ones(n)` returning how many 1 bits a non-negative integer has, using `%` and `//` (no `bin`). Set `c = ones(n)` for `n` = {n}.

```python starter
def ones(n):
    return 0

c = ones(n)
print(c)
```

```python solution
def ones(n):
    count = 0
    while n > 0:
        count += n % 2
        n //= 2
    return count

c = ones(n)
print(c)
```

```python tests
assert ones(0) == 0 and ones(1) == 1 and ones(255) == 8 and ones(1024) == 1, "ones should count the 1 bits: n % 2 is the lowest bit, and n // 2 shifts the rest down."
assert c == bin(n).count("1"), "c doesn't match for this lab's n."
```
:::

::: check
kind: python
given: bits = 8 | 10 | 12; vref = 2.5 | 3.3; f = 0.1 | 0.5 | 0.9

## Write `adc_volts(code, bits, vref)` returning `code * vref / 2**bits`, or `None` if `code` is outside 0 to `2**bits - 1`. Set `v = adc_volts(int(f * 2**bits), bits, vref)` and `bad = adc_volts(2**bits, bits, vref)`.

```python starter
def adc_volts(code, bits, vref):
    return code * vref / 2**bits

v = adc_volts(int(f * 2**bits), bits, vref)
bad = adc_volts(2**bits, bits, vref)
print(v, bad)
```

```python solution
def adc_volts(code, bits, vref):
    if code < 0 or code > 2**bits - 1:
        return None
    return code * vref / 2**bits

v = adc_volts(int(f * 2**bits), bits, vref)
bad = adc_volts(2**bits, bits, vref)
print(v, bad)
```

```python tests
import math
assert adc_volts(-1, 8, 5) is None and adc_volts(256, 8, 5) is None, "Codes outside 0 to 2**bits − 1 should give None."
assert math.isclose(adc_volts(128, 8, 5), 2.5), "adc_volts(128, 8, 5) should be 2.5 V."
assert bad is None, "2**bits is one past the largest code."
```
:::

::: check
kind: python
given: name = 1 | 2 | 3; x = 0.0123 | 4.567 | 1234.5; lo = 0.01 | 4 | 1000; hi = 0.02 | 5 | 2000

## Write `report(name, value, unit, lo, hi)` returning a line like `"TP3: 4.57 V ok"`: the name, a colon, the value to three significant figures, the unit, and `ok` if `lo <= value <= hi`, else `FAIL`. Set `line = report(f"TP{name}", x, "V", lo, hi)`.

```python starter
def report(name, value, unit, lo, hi):
    return name

line = report(f"TP{name}", x, "V", lo, hi)
print(line)
```

```python solution
def report(name, value, unit, lo, hi):
    status = "ok" if lo <= value <= hi else "FAIL"
    return f"{name}: {value:.3g} {unit} {status}"

line = report(f"TP{name}", x, "V", lo, hi)
print(line)
```

```python tests
assert report("TP1", 4.567, "V", 4, 5) == "TP1: 4.57 V ok", 'report("TP1", 4.567, "V", 4, 5) should be "TP1: 4.57 V ok".'
assert report("TP2", 5.2, "V", 4, 5) == "TP2: 5.2 V FAIL", 'Out of range should say FAIL: "TP2: 5.2 V FAIL".'
want = f"TP{name}: {x:.3g} V {'ok' if lo <= x <= hi else 'FAIL'}"
assert line == want, f"line should be {want!r}."
```
:::
:::
