A4 Python for engineers · Practice
Recursion, bisection and binary search
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 the recursive ladder(n, R): R for n = 0, otherwise R in series with (R in parallel with ladder(n - 1, R)). Set Req = ladder(n, R) for n = 3 and R = 100.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write bisect(f, a, b, tol=1e-12) and use it to set r to the root of between 0 and c (c = 3).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write first_at_least(xs, target) by binary search, returning the index of the first value at or above target in a sorted list (or len(xs) if none). Use it on the starter's E12 list, and set v to the value found (or None if there isn't one).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write a recursive parallel_all(rs) returning the resistance of a list of resistors all in parallel: one resistor is itself, and otherwise it's the first resistor in parallel with parallel_all of the rest. Then set Req = parallel_all(rs) for the list the starter picks.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A thermistor has with = 10 kΩ at = 298.15 K and = 3950 K. Using bisection between 250 K and 400 K, set T to the temperature (in kelvins, within K) where it reads Rm = 5000 Ω, and steps to how many halvings that took.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Bisection starts on an interval 4 wide and stops once the interval is narrower than . How many halvings does that take?
Check
Write a recursive power2(n) returning (with power2(0) = 1). Set p = power2(n).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Use bisection on to set r to (a = 5), to within .
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