A4 Python for engineers · Practice
Integers, floats and rounding error
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
Sample number n = 4230 goes into a circular buffer of length N = 16. Set slot to its position (0 to N − 1) and wraps to how many full times the buffer has filled before it.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write wrap(angle) that brings any angle in degrees into the range 0 to 360 (360 itself not included). Then set w to wrap(a) with a = 725.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
The code adds dt = 0.2 to a running total n = 10 times. Set same to whether the total matches n * dt, using math.isclose so that rounding error doesn't count as a difference.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Make a list times of every time from 0 to T = 2 s inclusive in steps of dt = 0.1 s, computing each from an integer counter so the last one is exactly T.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A 12-bit ADC turns a voltage from 0 to Vref = 2.5 V into a whole-number code from 0 to 4095, one step being Vref / 4096. Set lsb to one step in volts, v to the voltage for code c = 513 (c * lsb), and code to the code for x = 0.855 V: the nearest whole step, as an int, clipped to the range 0 to 4095.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A clock counts s = 33316 seconds since midnight. Set h, m and sec to the hours, minutes and seconds.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set r to v = 2.718 rounded to n = 1 decimal places.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set q to a // b and f to a / b, with a = 9 and b = 2, and set diff to f - q.
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