S1Q2 · Parse Equation and Solve for x¶
⚡ Quick Reference
Function: solve_for_x(equation: str) -> float
Core idea: split on x to get the coefficient of x and the constant; split on = to get the right-hand side. Solve ax + b = c → x = (c - b) / a.
def solve_for_x(equation: str) -> float:
equation = equation.replace(" ", "")
left, c = equation.split("=")
a_part, b_part = left.split("x")
a = float(a_part) if a_part not in ("", "+", "-") else float(a_part + "1")
b = float(b_part) if b_part != "" else 0.0
return (float(c) - b) / a
Key rules:
- Strip all spaces first with replace(" ", "")
- Split on "x" → left part is a, right part is b
- If nothing before x → a = 1; if just "-" before x → a = -1
- If nothing after x (before =) → b = 0
- Formula: x = (c - b) / a
Problem Statement¶
Problem
Write a function solve_for_x(equation: str) -> float that parses a linear equation of the form ax+b=c or ax-b=c and returns the value of x as a float. Spaces may appear anywhere and must be ignored.
Examples:
"2x +3= 11"
4.0
"-3x + 10=1"
3.0
"x + 2 = 5"
3.0
"2x=6"
3.0
Understanding the problem¶
The equation always has the form ax + b = c. The goal is to extract a, b, and c, then compute x = (c - b) / a.
The hint says to treat x and = as separators:
"2x+3=11"
↑ ↑
x =
Split on "=": left = "2x+3", right = "11" → c = 11
Split on "x": a_part = "2", b_part = "+3" → a = 2, b = 3
x = (11 - 3) / 2 = 4.0
Edge cases to handle¶
| Situation | Example | What it means |
|---|---|---|
No coefficient before x |
"x+2=5" |
a = 1 |
Negative coefficient before x |
"-x+2=5" |
a = -1 |
| No constant term | "2x=6" |
b = 0 |
| Negative constant | "5x-2=13" |
b = -2 |
The a_part special cases
After splitting on "x", the part before x can be:
"2"→a = 2""→a = 1(no coefficient means 1)"-"→a = -1(just a minus sign means -1)"+"→a = 1(just a plus sign means +1, rare but possible)
The cleanest fix: if a_part ends in a sign with no digits, append "1".
Tracing all examples¶
After removing spaces:
| Equation | Split on = |
Split on x |
a |
b |
c |
x = (c-b)/a |
|---|---|---|---|---|---|---|
"2x+3=11" |
"2x+3", "11" |
"2", "+3" |
2 | 3 | 11 | (11-3)/2 = 4.0 |
"5x-2=13" |
"5x-2", "13" |
"5", "-2" |
5 | -2 | 13 | (13-(-2))/5 = 3.0 |
"-3x+10=1" |
"-3x+10", "1" |
"-3", "+10" |
-3 | 10 | 1 | (1-10)/-3 = 3.0 |
"x+2=5" |
"x+2", "5" |
"", "+2" |
1 | 2 | 5 | (5-2)/1 = 3.0 |
"2x=6" |
"2x", "6" |
"2", "" |
2 | 0 | 6 | (6-0)/2 = 3.0 |
Solution approaches¶
def solve_for_x(equation: str) -> float:
equation = equation.replace(" ", "") # strip all spaces
left, c = equation.split("=") # split on =
a_part, b_part = left.split("x") # split on x
# handle coefficient of x
if a_part in ("", "+"):
a = 1.0
elif a_part == "-":
a = -1.0
else:
a = float(a_part)
# handle constant term
b = float(b_part) if b_part != "" else 0.0
return (float(c) - b) / a
Each edge case handled explicitly -most readable.
def solve_for_x(equation: str) -> float:
equation = equation.replace(" ", "")
left, c = equation.split("=")
a_part, b_part = left.split("x")
# if a_part is empty or just a sign, append "1"
if a_part in ("", "+", "-"):
a_part = a_part + "1"
a = float(a_part)
b = float(b_part) if b_part != "" else 0.0
return (float(c) - b) / a
Appending "1" to a bare sign ("" → "1", "-" → "-1") lets float() handle it cleanly.
import re
def solve_for_x(equation: str) -> float:
equation = equation.replace(" ", "")
# match: optional sign+digits before x, optional sign+digits after x, = sign+digits
match = re.match(r"([+-]?\d*)x([+-]\d+)?=([+-]?\d+)", equation)
a_str = match.group(1)
b_str = match.group(2)
c_str = match.group(3)
a = float(a_str) if a_str not in ("", "+", "-") else float((a_str or "") + "1")
b = float(b_str) if b_str else 0.0
return (float(c_str) - b) / a
Uses a regex pattern to capture the three parts in one pass. Overkill for this problem but useful if the equation format is more varied.
def solve_for_x(equation: str) -> float:
equation = equation.replace(" ", "")
left, c = equation.split("=")
a_part, b_part = left.split("x")
parse = lambda s, default: float(s) if s not in ("", "+", "-") \
else float((s or "") + "1") if s in ("", "+", "-") \
else default
a = float(a_part + "1") if a_part in ("", "+", "-") else float(a_part)
b = float(b_part) if b_part != "" else 0.0
return (float(c) - b) / a
A lambda encapsulates the sign-edge-case logic, making parse(a_part, 1.0) and parse(b_part, 0.0) self-documenting calls.
Key takeaways¶
Strip spaces first
equation.replace(" ", "") removes all spaces in one call before any parsing. Always sanitise input before splitting -spaces inside tokens break float().
Use the problem's natural separators
The hint says treat x and = as separators. split("=") then split("x") cleanly extracts all three parts without a complex parser.
Handle the implicit coefficient 1
When there's no digit before x, the coefficient is 1. The sign-append trick (a_part + "1") handles "", "+", and "-" uniformly.