Setting up Your Workflow for Competitive Programming in Kotlin

Get started in a matter of minutes!

Rishabh Malviya
Kt. Academy

--

A well-designed workflow is very important to get things done fast!

This is a follow-up to my previous article, where I convince you to try Kotlin for Competitive Programming. But if you’re already convinced, then this article is for you! It is a step-by-step guide for setting up your local environment.

What we are aiming for

Your local workflow should help you achieve a few different things:

  1. A consistent process for starting a new contest, and for creating new solution files.
  2. Quick access to repetitive code that is necessary for reading input and writing output in every problem
  3. Access to tools like the Intellisense, a Read-Eval-Print Loop (REPL), and a debugging interface.

The following setup ensures all of these things for you.

Setup: Step-by-Step Guide

Step 1

Download and install IntelliJ IDEA (Community Edition will do) on your system.

Step 2

Download the latest version of the kotlin-cp.zip file from this GitHub Releases page.

Step 3

Launch the IDE, and go to File -> Import Settings.

Import the settings file you downloaded in Step 2.

Click OK.

Restart Intellij IDEA.

Usage: Step-by-Step Guide

Let’s say you want to participate in this CodeForces contest (CodeForces Global Round 8).

Let’s walk through the typical workflow:

Step 1 — Creating a New Project for the Contest

Start by creating an appropriately named Kotlin project:

Step 2 — Creating a New File for a Problem

Now, let’s say you want to solve the first problem in the contest:

Create a new file to start writing up the solution. First select the src folder in the Project navigation pane:

Then press alt + N to create a new solution file (this shortcut comes from the settings you imported from the kotlin-cp.zip file earlier). Name the new file based on the problem title:

Digression: Understanding the Template (Skip if you Want)

Before you start coding up the solution, you’ll have to understand the template. You can see the full source code for the template here. It is based on the template in this blog post (by Spheniscine, on CodeForces).

At the top, you have a set of convenience functions for parsing console input:

The functions are all declared private, because each solution file must be self-contained. When you create a new file for the next problem, it will declare the same set of functions, but they will be private only to that file, just like these are private only to this file.

These input parsing functions are used in main:

The actual code to solve each test case is written in the solveTestCase() function of the solution class. Note that the name of the class is the same as the name of the file you provided earlier:

This class is declared private, in case there is a need to use the input parsing functions inside of it.

Okay, now back to the steps.

Step 3: Parsing the Console Input

Note that the first line of the main function takes care of reading the number of test cases and looping over them:

You only have to write the code to parse the individual test cases:

And provide the parsed input to the solveTestCase() function call:

Notice that an error shows up as soon as you provide the inputs to solveTestCase(). That’s because you haven’t written the definition of the function yet. It’s part of the next step.

Step 4 — Writing the Code to Solve the Problem

First things first, you need to change the function signature to match the call in the main function. This can be done from the ‘error bulb’ drop-down:

This will add the appropriate input arguments to the function solveTestCase():

Then change the output type for the function, and write out your solution:

Digression: Helpful Tools

If you want to run the code while providing input from the console, press Ctrl + Shift + F10:

If you want to debug the code, put in a break-point in the left gutter next to the line from where you want to start debugging, and press Shift + F9:

If you want to open the Kotlin REPL to quickly try out some code, you can do that by pressing Alt + K (this shortcut was added in the settings in the kotlin-cp.zip file):

Step 5 — Submit Your Solution

Since the code in the file is self-contained, submitting is easy.

On some websites, you would copy-paste the whole file contents into an editor. In CodeForces, you need to upload the file:

Conclusion

And that’s it!

It may seem long because I’ve tried to be as explicit as possible about all things you need to do. But follow these steps one or two times, and soon you’ll be doing it all of this in a matter of seconds.

Oh, and do take the time to get familiar with the debugger (Shift + F9) and the Kotlin REPL (Alt + K) later on — they are very helpful in practice.

Happy coding!

--

--

Knowledge shared is knowledge gained. | ML Platform Engineer @ Aquabyte.ai | Engineering Physics @ IIT Bombay | https://www.linkedin.com/in/rish-malviya