Last Updated: June 7, 2026
This is a dispatch problem dressed up as geometry. Pick the right pair of formulas based on the shape string, plug in the numbers from dims, and round the results. The work is clean branching and exact arithmetic, not an algorithm.
Two details decide whether your output matches. The first is the value of PI. Different libraries carry different precision, so the problem fixes PI = 3.14159 to make circle results deterministic. The second is the triangle's square root. Instead of a built-in sqrt, we compute the root with the Babylonian method so behavior stays identical across every language. That method starts from a guess and repeatedly averages the guess with the number divided by the guess, converging on the root in a few steps.
Once those two pieces are settled, each shape reduces to two one-line formulas.
shape is one of four known strings. You can branch with a simple if-else chain or a switch. There is no need to handle unknown shapes.dims is positive, so you never face a zero radius, a negative side, or a degenerate rectangle. The formulas apply directly without guarding against bad input.s * (s - a) * (s - b) * (s - c) stays non-negative, so the square root is always defined.The shape string tells you which formula to use, so the solution mirrors the problem: one branch per shape, each holding the two formulas that define it. The rectangle and square are direct multiplication and addition. The circle uses the fixed PI, and the triangle uses the Babylonian square root.
After computing the raw area and perimeter, round both to two decimal places and return them together.
PI = 3.14159 and a helper sqrt(x) that computes the square root with the Babylonian method."rectangle", set length = dims[0], width = dims[1]. Area is length * width, perimeter is 2 * (length + width)."square", set side = dims[0]. Area is side * side, perimeter is 4 * side."circle", set r = dims[0]. Area is PI * r * r, perimeter is 2 * PI * r."triangle", set a, b, c = dims[0], dims[1], dims[2]. Perimeter is a + b + c. Compute s = (a + b + c) / 2, then area is sqrt(s * (s - a) * (s - b) * (s - c)).[area, perimeter].Take the rectangle dims = [4, 3]:
The shape is "rectangle", so length = 4 and width = 3. Area is 4 * 3 = 12, and perimeter is 2 * (4 + 3) = 14. Rounded, the result is:
Now take the 3-4-5 triangle dims = [3, 4, 5]:
The perimeter is 3 + 4 + 5 = 12. For the area, s = 12 / 2 = 6, so the product under the root is 6 * (6 - 3) * (6 - 4) * (6 - 5) = 6 * 3 * 2 * 1 = 36. The square root of 36 is 6, so the area is 6. Rounded, the result is:
dims and apply a constant number of arithmetic operations. The Babylonian square root runs a fixed 50 iterations, which is also constant.