Lines
Lines (lesson 1.2.1)
0 of 9 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Quadratics
Quadratics (lesson 1.2.2)
0 of 10 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Coding exercise
Vertex and roots of any quadratic
Write two functions for , using the vertex rule and the quadratic formula from lesson 1.2.2.
vertex(a, b, c)returns the vertex as a tuple(x, y). It is the lowest point whena > 0and the highest whena < 0.real_roots(a, b, c)returns a list of the real roots, smallest first: two, one or none, as the discriminant says.
Both raise ValueError when a == 0, because then the expression is not a quadratic.
See the tests
def _raises_value_error(fn, *args):
try:
fn(*args)
except ValueError:
return True
return False
def test_vertex():
"""The vertex of x² − 6x + 5 is (3, −4)"""
expect_close(vertex(1, -6, 5), (3.0, -4.0), what="vertex(1, -6, 5)")
def test_vertex_downward():
"""The vertex of −x² + 6x is the maximum (3, 9)"""
expect_close(vertex(-1, 6, 0), (3.0, 9.0), what="vertex(-1, 6, 0)")
def test_two_roots():
"""x² − 6x + 5 = 0 has roots 1 and 5, smallest first"""
expect_close(real_roots(1, -6, 5), [1.0, 5.0], what="real_roots(1, -6, 5)")
def test_two_roots_negative_a():
"""−2x² + 2x + 4 = 0 has roots −1 and 2, still smallest first"""
expect_close(real_roots(-2, 2, 4), [-1.0, 2.0], what="real_roots(-2, 2, 4)")
def test_one_root():
"""x² − 4x + 4 = 0 has the single root 2"""
expect_close(real_roots(1, -4, 4), [2.0], what="real_roots(1, -4, 4)")
def test_no_real_roots():
"""2x² + 3x + 5 = 0 has no real roots: an empty list"""
roots = real_roots(2, 3, 5)
assert roots == [], f"expected [], got {roots!r}"
def test_not_a_quadratic():
"""a = 0 raises ValueError in both functions"""
assert _raises_value_error(vertex, 0, 2, 1), "vertex(0, 2, 1) should raise ValueError"
assert _raises_value_error(real_roots, 0, 2, 1), "real_roots(0, 2, 1) should raise ValueError"