2026, Jan 14 09:00
Avoid Wrong Answers on Online Judges: Trim Whitespace and Robustly Output ABC Orders in Python
Why your Python solution passes locally but fails on the online judge: handle whitespace in the ABC pattern. Learn the .strip() fix and a tolerant approach.
Reordering three numbers sounds trivial until an online judge decides to be creative with input formatting. Here’s a compact guide to a subtle failure mode: when your logic is correct, your tests are green locally, but the judge still marks answers wrong. The root cause here is not algorithmic—it’s input hygiene.
Problem recap
Given three positive integers representing A, B, C in arbitrary order, the task is to print them sorted so that A < B < C, then output the values in an order specified by a three-letter string like ABC, ACB, etc. The expected output is the numbers in that requested sequence, space-separated.
Code that “works locally” but fails on the judge
The following program sorts the numbers and branches on the requested order. It looks fine and will pass local samples, yet it only clears a small portion of the judge’s tests.
nums = list(map(int, input().split()))
layout = input()
nums.sort()
if layout == "ABC":
print(nums[0], nums[1], nums[2])
elif layout == "ACB":
print(nums[0], nums[2], nums[1])
elif layout == "BAC":
print(nums[1], nums[0], nums[2])
elif layout == "BCA":
print(nums[1], nums[2], nums[0])
elif layout == "CAB":
print(nums[2], nums[0], nums[1])
else:
print(nums[2], nums[1], nums[0])What actually goes wrong
The order string coming from the judge can contain extra whitespace around the letters. That means values like "ABC", "ACB", etc. don’t compare equal if there are leading or trailing spaces. Your comparisons fail, and the logic falls into the final fallback branch. If you replace the final else with a specific condition like elif layout == "CBA", then none of the branches match and nothing is printed at all, which explains why it fails every checkpoint in that variant.
This behavior isn’t mentioned in the problem statement, so the mismatch is not about your algorithm; it’s about how the online judge provides input. A small normalization step for whitespace makes the solution robust.
Minimal fix
Strip the order string before comparing. That’s enough to align with the judge’s format.
nums = list(map(int, input().split()))
layout = input().strip()
nums.sort()
if layout == "ABC":
print(nums[0], nums[1], nums[2])
elif layout == "ACB":
print(nums[0], nums[2], nums[1])
elif layout == "BAC":
print(nums[1], nums[0], nums[2])
elif layout == "BCA":
print(nums[1], nums[2], nums[0])
elif layout == "CAB":
print(nums[2], nums[0], nums[1])
else:
print(nums[2], nums[1], nums[0])An alternative that’s tolerant by design
Another approach is to iterate through the characters of the requested pattern and map A, B, C to the corresponding sorted values. This naturally ignores any characters that aren’t A, B, or C, which is why it succeeds even when whitespace sneaks in.
values = list(map(int, input().split()))
design = input()
values.sort()
picked = []
for ch in design:
if ch == "A":
picked.append(values[0])
elif ch == "B":
picked.append(values[1])
elif ch == "C":
print(" ".join(map(str, picked)))Why this matters
The specification didn’t advertise stray spaces, yet the real input did. In environments like online judges, CI pipelines, or any automated ingestion, defensively handling minor format deviations can be the difference between a correct algorithm and a “Wrong Answer.” When the cost is a single .strip(), it’s a pragmatic win. And when input comes from interactive users, a bit of tolerance often saves support time.
Takeaways
Normalize inputs that are meant to be exact matches when it’s cheap to do so. If the format is tiny and controlled, trimming whitespace is a low-risk improvement. Where possible, structure the logic to be inherently robust—iterating over expected tokens and ignoring unknown ones is a simple way to avoid brittle equality checks. In short, the algorithm is only as reliable as the inputs it consumes, and a little hygiene goes a long way.