πŸ› οΈToolsShed

Pemecah Proporsi

Selesaikan proporsi dan rasio β€” temukan nilai yang hilang dalam a/b = c/d.

Proporsi: a / b = c / d

/
=
/

Biarkan satu kolom kosong untuk diselesaikan

Pertanyaan yang Sering Diajukan

Implementasi Kode

def solve_proportion(a, b, c, d):
    """
    Solve a/b = c/d for the missing value (pass None for the unknown).
    Returns the computed value.
    """
    values = [a, b, c, d]
    unknowns = [i for i, v in enumerate(values) if v is None]
    if len(unknowns) != 1:
        raise ValueError("Exactly one value must be None")

    idx = unknowns[0]
    a, b, c, d = values

    if idx == 0:   # x/b = c/d  ->  x = b*c/d
        return b * c / d
    elif idx == 1: # a/x = c/d  ->  x = a*d/c
        return a * d / c
    elif idx == 2: # a/b = x/d  ->  x = a*d/b
        return a * d / b
    else:          # a/b = c/x  ->  x = b*c/a
        return b * c / a

# Example: 3/4 = x/8  ->  x = 6
result = solve_proportion(3, 4, None, 8)
print(f"x = {result}")  # x = 6.0

# Scale recipe: 2 cups for 4 servings, how many for 10?
cups = solve_proportion(2, 4, None, 10)
print(f"{cups} cups for 10 servings")  # 5.0 cups for 10 servings

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.