Competitive Programming setup in VS-Code
Tags: vscode, competitive, programming
Published: 2020-08-05 15:16:30


### 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:

   ```txt
   This is line 1
   This is line 2
   ```

4. Download [code-runner](https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner)  extension for VSCode and restart VSCode

5. Go to settings of VSCode with `Cntr + ,`

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

7. Set `"code-runner.saveFileBeforeRun"` as `true`

8. Keep this sfile open for further modification



# 2. C++ Setup

1. Download and install [MinGW-w64 for Windows](https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/installer/mingw-w64-install.exe/download) - compiler for C++
2. Execute `g++ --version` inside PowerShell to check if compiler is correctly installed

3. Paste following code inside `app.cpp`

```c++
#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;
}
```



4. If yo are using Windows, Open PowerShell using `Cntr + ~` (Choose 'PowerShell' as Default shell if not already) and run the following command

```powershell
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
```

**OR**

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


# 3. Java Setup

1. Download and Install [Java](https://java.com/en/download/) - 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

```java
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>"
	}
}

```


4. For Windows, open PowerShell and run command as - 
```powershell
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
```

4. For Linux or MacOS, Open Bash and run command as -
```bash
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
```



# 4. Python Setup

1. Download and Install - [Python](https://www.python.org/downloads/) 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`

```python
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>"
```

4. For Windows, open PowerShell and run - 

```powershell
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'
```

4. For Linux or MacOS, open Bash and run -

```bash
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
```



# 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
```json
"cpp": "g++ $fileName -o $fileNameWithoutExt; Get-Content input.txt | .\\$fileNameWithoutExt",
"java": "javac $fileName; java $fileNameWithoutExt",
"python": "Get-Content .\\input.txt | python .\\app.py",
```

- For Linux or Bash
```json
"cpp": "g++ $fileName -o $fileNameWithoutExt && cat input.txt | ./$fileNameWithoutExt",
"java": "javac $fileName && java $fileNameWithoutExt",
"python": "cat input.txt | python app.py",
```

3. Now come to the file `app.cpp` or `app.java` or `app.py` file
4. 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.