Skip to main content

Input and Output

Though there are many different types of problems on the CCC, all of them have one thing in common -- you read some data from standard input, solve the problem given the input, and output an answer to standard output.

note

We list only the most common and general approaches to reading input here for simplicity. On certain CCC problems, the input size may be large, in which case you should use fast I/O.

Reading Input

In Python, use the input() function to read one line from standard input.

line = input()

If an integer/float value is desired, convert it manually.

i = int(input())
f = float(input())

Sometimes, input data will contain several items on a single line. For example, a problem might provide several integers separated by whitespace, like such:

1 2 3 4 5

In these cases, use the str.split method to break the string into its components.

parts = input().split(" ")
# parts = ["1", "2", "3", "4", "5"]

This may be written more succintly as

parts = input().split() # splits around whitespace
# parts = ["1", "2", "3", "4", "5"]

Note that str.split will return a list of strings, while we might wish to work with a list of integers instead. The map function can accomplish this:

ints = map(int, input().split())
# parts = [1, 2, 3, 4, 5]
danger

When solving problems, do not issue a prompt for input unless it is explicitly stated that you should do so in the problem statement. This is because the CCC grader will treat your prompt as output and therefore mark it as incorrect.

For example, say we are solving a problem which takes a single input, the age.

Correct

age = int(input())
# ...

Incorrect

age = int(input("What is your age?"))

# Also wrong:
print("What is your age?")
age = int(input())

Writing Output

In Python, use the print function to write to standard output:

print("Hello world") # Hello world

name = "Joe"
print("Hello", name) # Hello Joe

print(1, 2, 3) # 1 2 3

print will add a newline to the end by default. To avoid this, provide a different argument to the end parameter:

print("Hello world", end='') # No newline

To print a sequence of items, map into a sequence of strings and then use str.join:

names = ["Vincent", "Andrei", "Joe"]
print(" ".join(names)) # Vincent Andrei Joe

xs = [1, 2, 3]
print(" ".join(map(str, xs))) # 1 2 3

Exact Output

Can you spot the difference between these two outputs?

1 2 3 
1 2 3
Click to reveal

The first output has a trailing space after the 3.

Unless otherwise specified, output must be exact!

That is, outputting the first text instead of the second in the example given above would be marked incorrect. No matter how miniscule, unless otherwise noted, any difference between your output and the expected output results in your solution being marked incorrect.

Make sure your output matches the requirements exactly. Some things to check for:

  • Are you outputting periods where you're not supposed to (or vice versa)?
  • Are you outputting strings in the wrong case (Yes instead of yes)?
  • Are you outputting an extra newline somewhere?