A46 of 56
0 XP
  1. Hook
  2. Problems

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.

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.