A4 Python for engineers · Practice
More about functions
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 divider(Vs, R1, R2=10e3) returning the output voltage, with R2 defaulting to 10 kΩ. Then set a = divider(Vs, R1) and b = divider(Vs, R1, R2=R1), with Vs = 5 and R1 = 1500.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write polar(x, y) returning the pair (r, theta) with theta in degrees from math.atan2. Then set r, theta = polar(x, y) for x = 5 and y = 4.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write average(f, a, b, n=1000) that averages f over n equally spaced midpoints of [a, b]. Then set m = average(lambda t: t**p, 0, 1) with p = 3.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write rc_corner(R, C, unit="Hz") returning the corner frequency in hertz, or when unit="rad/s". Set fc = rc_corner(R, C) and wc = rc_corner(R, C, unit="rad/s").
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write make_divider(R1, R2) that returns a function: given a source voltage, that function returns the divider's output . Then set div = make_divider(R1, R2) for R1 = 3900 Ω and R2 = 1500 Ω, and out = div(Vs) for Vs = 12 V.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write resistor(V, R) returning (I, P). Set I, P = resistor(V, R).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write twice(f, x) that returns f(f(x)). Set y = twice(lambda t: t * k, 1) with k = 3.
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