A4 Python for engineers · Practice
Iteration patterns
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
For the sweep the starter picks, set fpk to the frequency where the gain g is largest.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set best to the value in values (from the starter) nearest target = 3600, using min with a key.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set ups to the list of indexes k where the samples cross zero going up, between sample k and k + 1 (sample k below 0, sample k + 1 at or above 0).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set ok to whether every reading is below lim = 5.3 V, and any_low to whether any reading is below 4.75 V, using all and any.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Using one list comprehension with enumerate, set bad to the list of (index, value) pairs for the readings outside lo = 4.75 to hi = 5.1 V (the edges count as in range). Then set n_bad to how many there are.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Make bom a dictionary from the two lists names and vals with dict(zip(...)).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set by_value to the parts sorted from smallest value to largest.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set diffs to the list of differences between neighboring samples, x[k+1] - x[k].
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