DEV Community

loading...

Competitive Programming setup in VS-Code

youngmahesh profile image Mahesh Updated on ・4 min read

Note

  • Programming Languages Covered - C++, Java, Python
  • Code Editor used - VSCode
  • Operating Systems covered - Windows, Linux, MacOS

Target

Create an environment such that -

  1. We could write 'input' inside a text-file
  2. Click on a icon
  3. And 'output' of the code will be shown in Terminal or will be written in a text-file

1. Create Folder

  1. Create a new folder on desktop and open this folder inside VSCode

  2. Create app.cpp, app.java, app.py in the folder

  3. Also create input.txt in the folder with following text:

   This is line 1
   This is line 2
Enter fullscreen mode Exit fullscreen mode
  1. Download code-runner extension for VSCode and restart VSCode

  2. Go to settings of VSCode with Cntr + ,

  3. Click on top-right 'file-icon' (to open settings in JSON)

  4. Set "code-runner.saveFileBeforeRun" as true

  5. Keep this sfile open for further modification

2. C++ Setup

  1. Download and install MinGW-w64 for Windows - compiler for C++
  2. Execute g++ --version inside PowerShell to check if compiler is correctly installed

  3. Paste following code inside app.cpp

#include <bits/stdc++.h>
using namespace std;

int main() {

   string word1, word2;             // memory-space for words
   cin >> word1 >> word2;           // insert first-word => word1 && second-word => word2
   cout << word1 << " " << word2;   // output "<word1> <word2>"

   return 0;
}
Enter fullscreen mode Exit fullscreen mode
  1. If yo are using Windows, Open PowerShell using Cntr + ~ (Choose 'PowerShell' as Default shell if not already) and run the following command
g++ app.cpp -o app; Get-Content input.txt | .\app   # write output in the powershell-termainal
# g++ app.cpp  => compile 'app.cpp' with g++
# -o app       => defining output file-name as 'app.exe'
# ;            => end of command
# Get-Content input.txt | .\app  => Execute 'app.exe' with content from 'input.txt'

# OR

g++ app.cpp -o app; Get-Content input.txt | .\app > output.txt   # write output inside 'output.txt' file
Enter fullscreen mode Exit fullscreen mode

OR

  1. If your are using Mac or Linux, Open Bash/Terminal using Ctrl + ~ and run the following command
g++ app.cpp -o app && cat input.txt | ./app > output.txt      
Enter fullscreen mode Exit fullscreen mode

3. Java Setup

  1. Download and Install Java - for compilation of Java Code
  2. Execute java --version inside PowerShell to check if compiler is installed correctly

  3. Paste following code inside app.java file

import java.util.*;
import java.lang.*;
import java.io.*;

/* IMPORTANT: class-name must be equal to file-name, here file-name is 'app.java', hence class-name is 'class app' */

class app
{
    public static void main (String[] args) throws java.lang.Exception
    {
      File input = new File("./input.txt");  // declare input
        Scanner scanner1 = new Scanner(input);  // declare scanner

      String word1 = scanner1.next();         // scan first-word
      String word2 = scanner1.next();         // scan second-word

      String word3 = word1 + " " + word2;
      // String line1 = sc1.nextLine();      // in-case if you want to scan complete line

      System.out.print(word3);         // output "<word1> <word2>"
    }
}

Enter fullscreen mode Exit fullscreen mode
  1. For Windows, open PowerShell and run command as -
javac app.java; java app    # output in the powershell-terminal
# OR
javac app.java; java app > output.txt   # write output inside 'output.txt'

# javac app.java   =>  compile '.java' file using javac into bytecode as '.class'
# ;                =>  end of previous-command
# >                => transfer output genereated to the a file
# java app         =>  execute '.class' file
Enter fullscreen mode Exit fullscreen mode
  1. For Linux or MacOS, Open Bash and run command as -
javac app.java && java app        # write output in bash-terminal
#OR
javac app.java && java app > output.txt  # write output in output.txt file

# javac app.java    => compile app.java to app.class
# &&                => and, bridge between two-separate commands
# java app          => execute '.class' file
Enter fullscreen mode Exit fullscreen mode

4. Python Setup

  1. Download and Install - Python for compilation of python code

  2. Execute python --version inside PowerShell to check if Python is correctly installed

  3. Paste following code into app.py

line1 = input()         # get 'first-line' as value of variable 'line1'
line2 = input()         # get 'second-line' as value of variable 'line2'

arr1 = line1.split()    # get array of individual words from 'line1'

print(arr1[0] + ' ' + arr1[1])  # output => "<word1> <word2>"
Enter fullscreen mode Exit fullscreen mode
  1. For Windows, open PowerShell and run -
Get-Content .\input.txt | python .\app.py       # output will print inside 'powershell'
Get-Content .\input.txt | python .\app.py > output.txt  # ouput will be written inside 'output.txt'
Enter fullscreen mode Exit fullscreen mode
  1. For Linux or MacOS, open Bash and run -
cat input.txt | python app.py     # write output insided terminal
cat input.txt | python app.py > output.txt    # write output inside file named - output.txt in the same folder
Enter fullscreen mode Exit fullscreen mode

5. Execute PowerShell/Bash Commands with One-Click

  1. Open VSCode settings (inside JSON format) as described in the Step-1
  2. Comment out code for cpp, java and python inside "code-runner.executorMap": {} and add the json-code given below:
  • For WIndows
"cpp": "g++ $fileName -o $fileNameWithoutExt; Get-Content input.txt | .\\$fileNameWithoutExt",
"java": "javac $fileName; java $fileNameWithoutExt",
"python": "Get-Content .\\input.txt | python .\\app.py",
Enter fullscreen mode Exit fullscreen mode
  • For Linux or Bash
"cpp": "g++ $fileName -o $fileNameWithoutExt && cat input.txt | ./$fileNameWithoutExt",
"java": "javac $fileName && java $fileNameWithoutExt",
"python": "cat input.txt | python app.py",
Enter fullscreen mode Exit fullscreen mode
  1. Now come to the file app.cpp or app.java or app.py file
  2. Click on the 'horizontal-rectangle' icon on 'top-left' => Now you can see the 'output' of your code inside PowerShell/Bash terminal

In this way you can test your code with just 'one-click' (with writing input inside 'input.txt' file) before submitting on 'Competitive Programming Platform'.

If you find any problems or want to suggest improvements, please let me know in comments and I will modify this article.

Discussion (0)

pic
Editor guide