Code

Measure the Depths

The math module holds the classic number tools.

import math

math.ceil(2.1)        # 3     (round up)
math.floor(2.9)       # 2     (round down)
math.sqrt(81)         # 9.0   (square root)
math.pi               # 3.14159...
math.prod([2, 3, 4])  # 24    (multiply them all)

Comparing decimals safely

Computers store decimals in binary. That means tiny errors can creep in:

0.1 + 0.2 == 0.3               # False!  (0.30000000000000004)
math.isclose(0.1 + 0.2, 0.3)   # True

math.isclose asks "are these two numbers almost equal?" Use it whenever you compare floats.

Assignment

  1. crates_needed(items, per_crate): return how many crates hold items. Round up with math.ceil.
  2. lock_combinations(dials): dials is a list of how many symbols each dial has. Return how many combinations the lock has: all the numbers multiplied together. Use math.prod.
  3. same_reading(a, b): return True if two float readings are equal according to math.isclose.