A4 Python for engineers · Practice
Problem set 2: the basics
Problems dealt one at a time, with new numbers every time. The kinds you miss come back more often, and you can keep going as long as you like.
Check
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 = 1000 mAh and I = 0.5 mA.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
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 = 0.8 V.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Add up for , stopping before the first term smaller than tol = 1 × 10⁻⁸. Set total to the sum and n to how many terms you added.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A diode and a resistor R = 220 Ω are in series across Vs = 5 V. The diode's voltage is . Starting from , repeat until changes by less than A. Set I to the result and Vd to the diode's voltage then.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write ones(n) returning how many 1 bits a non-negative integer has, using % and // (no bin). Set c = ones(n) for n = 95.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
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 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
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"TP2", x, "V", lo, hi).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
A kind of problem counts as solid once you've got it right on the first try twice in a row. Back to the lesson · Practice every topic in A4