A4 · Problem set 1
Problem set 1: numbers and strings
These labs use lessons 1 to 5: values and functions, integers and floats, strings, and bits and bytes. Each one is a small piece of the bookkeeping engineers do every day, from reading a resistor's value to formatting a measurement. When a check fails, read the hidden test's message: it says which case went wrong.
Check
Write parse_value(s) that reads a value written with an SI prefix (p, n, u, m, k, M or G as the last character, or none) and returns it as a float. Set x = parse_value("680u").
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write eng(x, unit) returning a string like "4.7 kΩ": the value scaled to a number from 1 up to (not including) 1000 with the matching prefix (p, n, µ, m, none, k, M, G), shown with :g, a space, then the prefix and unit. Set s = eng(v, "Ω") for v = 3.3 × 10⁻⁷.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Add 0.1 to a running total n = 10000 times in a loop, starting from 0.0. Set total to the result, err to abs(total - n / 10), and exact to math.fsum of the same n values (which rounds only once).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write hms(seconds) returning a string "h:mm:ss" (hours with no padding, minutes and seconds with two digits) using // and %. Set s = hms(T) for T = 3600.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write sig(x, digits) rounding a positive number to digits significant figures with round and math.log10. Set y = sig(x, d) for x = 98765 and d = 2.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A 16-bit status register packs fields: bits 0 to 3 are the channel, bits 4 to 7 the gain code, and bit 15 an error flag. Write fields(reg) returning (channel, gain, error) using >> and &, with error a bool. Set f = fields(reg) for reg = 1234, and h = f"{reg:04X}".
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A resistor's first two color bands are digits and the third is a power-of-ten multiplier (black 0, brown 1, red 2, orange 3, yellow 4, green 5, blue 6, violet 7, gray 8, white 9). Write ohms(bands) taking a string of three color names separated by spaces and returning the resistance in ohms. Set r = ohms("orange white yellow").
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
5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.