Kattis is a website that allows the automagic testing of your solutions to problems. This is where you will go to find problems to work on. Click here to access the Camp Inc. Kattis site.
Registration for Kattis
Visit the Camp Inc. Kattis site and click the Sign In link in the top right. In the likely event you don’t already have a Kattis account, you will need to register for one. You will need to use your email address to register.
Once you’ve registered for Kattis, you can simply join the Summer 2016 site using the course key provided by me. I haven’t posted it here to prevent random internet people from registering.
Submitting Kattis Problems
Visit the problem page and click the green submit button. Upload your solution, and check to make sure it was accepted!
Tips for Kattis Problems
Kattis gives you your input just like a user would, and checks for the answer
in your program’s output, which means you can use the regular
print() functions to handle this.
Kattis does not need input prompts, after all, she is a computer program. This
means you can leave the arguments to
input() blank. Putting anything in the
prompt will confuse her. Here’s an example:
number = int(input()) print(number)
Handling Multiple Test Cases
Often times problems on Kattis will have you run multiple test cases. The simple solution is to put each case in a loop. Here’s an example:
Say you are given an input with the first line being N, the number of test cases. Then, N lines follow, and each line has a word to convert from lower to upper case. Here is an example input:
7 all your base are belong to us
Then here is an example solution:
N = int(input()) for case in range(N): # Handle each test case here word = input() print(word.upper())
Handling Multiple Integers Per Line
Often times Kattis problems tend to give you multiple integers per line.
There’s a few ways to handle this, but you’re going to have to use
.split() on a string will give us a list of strings,
created by whitespace separating our original string.
As you probally guessed, this gives us one way already to get multiple inputs on the same line:
line = input().split() a = int(line) b = int(line) c = int(line) # ... do something
However, this is a bit cumbersome. We can use the
map function to tidy this
map takes a function, and an iterable. It calls the function on each
element of the iterable, and returns an iterable with each element called.
a, b, c = map(int, input().split()) # ... do something
Much cleaner! You may be wondering why we were able to write
a, b, c before
the equals on line 3. Python allows assignment of multiple variables at once
if the item on the right is an iterable.
As another alternative, you could use an inline
for loop to generate a list
where each item is converted to an integer. Here is an example:
my_list = [int(x) for x in input().split()] # ... do something
You of course could use Python’s multiple assignment with this:
a, b, c = [int(x) for x in input().split()] # ... do something
map and inline
for loops (actually called generator expressions)
interest you, you may be interested in reading
this page in the Python
documentation, which details all of the above, and more.
Treating the Input as a File
Sometimes it is useful to treat the input as a file you can
sys.stdin is exactly what you are looking for. You will need to
import sys to do that.
import sys lines_list = sys.stdin.read().splitlines() # Now lines_list is a list of strings, each a line from input
You could also use it like this:
import sys for line in sys.stdin: # now we are iterating over each line in input