Skip to content
CCC Python Course

Output edge cases and special checkers

Module
C.8
Lesson
1 of 1
Reading time
5 min

In this lesson

  • Recognize when the grader uses a special checker instead of exact string matching.
  • Identify common output edge cases (trailing newlines, precision, order).
  • Write code that produces output in the exact format the problem specifies.

You have written a solution. It computes the right answer. But when you submit, you get Wrong Answer. The logic is correct. The issue is that your output format does not match what the problem asks for. This happens more often than you might think, especially with floating-point numbers, trailing whitespace, or when the problem accepts multiple correct answers.

What goes wrong with output

Your algorithm computes 3.14159 but the problem asks for the answer rounded to two decimal places. You print 3.14159 and get Wrong Answer. The judge is correct. You did not print what it asked for.

Your algorithm correctly solves the problem and produces all the right numbers. But you print them in one order and the judge expects a different order. If the problem says "print the elements in any order", most judges have a special checker that accepts any permutation. But if it says "print the elements in sorted order" and you print a different order, you get Wrong Answer.

You print each line of output and forget the trailing newline. The judge expects "Hello\n" and you print "Hello". The judge rejects it. This seems like a small thing, but many judges are strict about it.

Floating-point output is tricky. If you print 0.3333333 but the judge expects 0.333 (three decimals), it is Wrong Answer. Some problems ask for the answer to the nearest integer. Others ask for a specific number of decimal places. Read the problem carefully.

When the judge uses a special checker

Some problems have multiple correct answers or answers that are hard to compare exactly. The judge uses a custom checker to decide whether your output is correct. For example, a geometry problem might ask for a point that satisfies some condition. There may be infinitely many correct answers. The judge's checker verifies that your answer actually satisfies the condition, rather than comparing it to one expected answer.

How do you know if the judge uses a special checker? The problem statement often says things like "any correct answer will be accepted" or "your output will be judged with a special checker". If it says this, you do not need to match a specific expected output exactly. You only need to produce an answer that satisfies the conditions in the problem.

But if the problem says "print the answer", it probably expects a single specific answer.

Common output formats

Read the output format section of the problem carefully. It often has examples. Here are common formats.

Single integer: print the number followed by a newline. In Python, print(n) does this.

Multiple integers on one line: print(" ".join(map(str, numbers))). Make sure there is no trailing space. print(*numbers) also works in Python.

Multiple lines: print each item on its own line. print("\n".join(map(str, items))) works, or loop with print(item) for each.

Floating-point numbers: if the problem specifies precision, use formatting. f"{value:.2f}" gives two decimal places. f"{value:.6f}" gives six. Count the decimal places in the example output and use that.

Order matters: if the problem says "print in increasing order", sort before printing. If it says "in any order", use a special checker, or the problem gives many examples showing that the order varies.

Trailing newline: always include it. print() in Python adds it automatically.

examples/format_demo.py
"""Four common output shapes, each written the way a judge expects it."""
# A single integer.n = 42print(n)
# Several integers on one line, one space between them, no trailing space.numbers = [1, 2, 3, 4, 5]print(" ".join(map(str, numbers)))
# A float rounded to a fixed number of decimal places, not Python's default.value = 3.14159print(f"{value:.2f}")
# One item per line, with no blank line at the end.items = ["apple", "banana", "cherry"]print("\n".join(items))

Output

42
1 2 3 4 5
3.14
apple
banana
cherry
Four common output shapes: a single number, several on one line, a rounded float, one item per line

How to debug output format

If you get Wrong Answer and you think your algorithm is right, test the exact output format.

Read the output section of the problem statement word by word. Note every detail: number of decimal places, "in sorted order", "each on its own line", "separated by space", "one space between", "no trailing space". Some problems specify precision as "absolute error at most 10^-6" or "relative error at most 10^-6". These mean different things. Absolute error allows your answer to be off by a small fixed amount. Relative error means your answer can be off by a small percentage of the true answer. Read carefully.

Run your code on the sample input and print the output. Compare it character by character to the expected output. Check for trailing spaces, missing newlines, wrong precision. Use repr() in Python to see exact whitespace: print(repr(your_output)) and print(repr(expected_output)) side by side.

A common mistake: your output is "3 4 5\n" but the expected output is "3 4 5" with no trailing newline. This is rare in CCC, but it happens. Print the outputs with repr() to catch this.

Another mistake: you print "3.14" but the problem asks for "3.140000". The number is right but the format is wrong. Use format strings: f"{value:.6f}" forces exactly six decimal places, padding with zeros if needed.

If the output format is correct but you still get Wrong Answer, the logic is wrong, not the format.

A worked example: debugging a format error

A problem asks for the sum of all integers in a list, printed to exactly three decimal places. Your solution computes 42 as the sum. You print it with print(42). The judge returns Wrong Answer. You think the logic is wrong, but the logic is right. The format is wrong.

You check the problem statement: "Output the sum as a floating-point number with exactly three decimal places." You rewrite: print(f"{sum_value:.3f}"). Now 42 becomes "42.000". You run on the sample input. The output matches. You submit. AC.

This example shows how easy format issues are to spot once you know to look. The logic was never wrong. A one-line fix solved it.

Another example: a problem asks for the three vertices of a triangle. You compute the vertices as (1.5, 2.7) and (3.2, 1.1) and (4.0, 5.5). You print each on its own line with print(x, y). This outputs "1.5 2.7" on the first line. The judge says Wrong Answer. You check the output format: "Output x and y separated by a single space, each to two decimal places." You change to print(f"{x:.2f} {y:.2f}"). Now each line is "1.50 2.70", "3.20 1.10", "4.00 5.50". The judge accepts it.

Format errors cluster around a few patterns: wrong precision, wrong spacing, wrong order, missing newlines, trailing spaces. Once you learn to recognize them, Wrong Answer from format is easy to fix.

Recap

Read the output format section of the problem carefully. Test your output against the sample using exact character-by-character comparison. Check for trailing spaces, missing newlines, and precision. Some problems use a special checker that accepts multiple answers. If the problem says so, verify that your answer satisfies the conditions, not that it matches a single expected output exactly. Format errors are easy to fix once you spot them.