A4 · Problem set 7
Problem set 7: arrays
These labs use lessons 28 to 30: NumPy arrays, vectorized arithmetic and two-dimensional arrays. Each one replaces a loop over measurements with a few array operations. Write them without Python loops where you can; the hidden tests check the results, and the message says which case went wrong.
Check
The starter has an array R of resistor values in ohms. Each sits alone across V = 5 V. Set P to the array of their powers, total to the total power, and worst to the index of the resistor that dissipates the most, without loops.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The starter makes noisy samples v. Using a Boolean mask, set frac to the fraction of samples above th = 1 V, mean_above to the average of just those samples, and n_neg to how many are negative.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A capacitor discharges into a 10 Ω resistor from 10 V with time constant tau = 2 ms. The starter samples the power every 10 µs for 10 time constants. Set E to the energy delivered, by the trapezoid rule written with array slices (no np.trapz), and exact to with .
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A thermal camera's image of a board is the 2-D array T in °C (made in the starter). Set hot to the (row, col) of the hottest pixel using np.argmax and np.unravel_index, row_means to the mean of each row, and over to how many pixels in row r = 2 exceed 60 °C.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Using broadcasting, build ratio, a table of with down the rows and across the columns, both from the starter's E12 list (1 kΩ to 8.2 kΩ). Set n_good to how many pairs land within 1% of the target k = 0.4, and best to the (R1, R2) pair closest to it.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A sine of amplitude A = 6 V goes through a clipper that limits it to ±L = 2.5 V. Using np.where (or np.clip), set out to the clipped samples, n_clipped to how many samples were changed, and rms_out to the RMS of the output.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A logic signal is sampled every microsecond. Threshold it at th = 1.65 V into 0s and 1s, then use np.diff to set rises to the sample indices where it goes from 0 to 1 (the index of the first 1), and width to the average number of samples each high pulse lasts.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Have paper and a calculator ready. Work each problem out by hand, then enter the answer. Your first try on each one counts toward your score.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.