Online Compiler — Demonstration of SubProcess
I learned the subprocess module in python. But, during the learning cycle, I was implementing the logic with no real-life implementation.
As a software developer, to achieve this position every individual has to go through various Data structure and Algorithm problems. We grill Leetcode, Codeforces, Codechef, HackerEarth, and N number of platforms. But I wondered how these platforms can compile 20–30 programming language codes and provide the results respectively.
Just take an example that an XYZ platform is hosted on an Apache Tomcat webserver. How during runtime it can compile my C lang code provide its input and in the response return the respective output or if it fails to compile how errors are visible to us in a very simple manner?
In a very simple manner, I demonstrated this by developing a platform that can execute C, C++, Python, and Java code with custom inputs.
Let us test this application with a code to print all even values existing in a vector. Source code written in C++
//Example1.cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v={1,2,3,4,5,6,7,8,9,0};
for(int e:v)
{
if(e%2==0) cout<<e<<" ";
}
cout<<endl;
return 0;
}
Now, knowingly I will put an error in the code by missing a semi-colon in for loop.
//Example2.cpp
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v={1,2,3,4,5,6,7,8,9,0};
for(int e:v)
{
if(e%2==0) cout<<e<<" "
}
cout<<endl;
return 0;
}
Now, we can try a python code.
#Code to determine maximum subarray sum
def maxSubArraySum(inp):
mx=-2147483648
sm=0
for i in inp:
sm+=i
mx=max(sm,mx)
if sm<0: sm=0
return mx
print(maxSubArraySum([1,-3,4,-1,-2,6]))
#Output should be 7 -- > [4,-1,-2,6] sum=7
Now we can try with a custom test case.
#Code to determine maximum subarray sum
def maxSubArraySum(inp):
mx=-2147483648
sm=0
for i in inp:
sm+=i
mx=max(sm,mx)
if sm<0: sm=0
return mx
n=int(input())
inp=[]
for i in range(n):
inp.append(int(input())
print(maxSubArraySum(inp))
//Text file containing testcase
9
5 -4 -1 11 4 -5 9 -14 -2
//Output should be 19
// [5,-4,-1,11,4,-5,9] sum=19
How all this was possible?
The idea behind this was simple — “I will determine the source code file, which type of file it is, is it a .py file or .cpp file or .c file or .java file or .js file or any other language. Behind I will run a subprocess which will be the compiler to whom I will pass the source file as input then it will compile and if the respective language is a complied language then an executable will be generated or else it would be interpreted language then respective decision can be made. If there is an error I will send an error in response else I will be sending the output generated by the code.”
The thing which I made is very simple but it can be powerful when I will introduce features like,
- Time constraints to tackle time limits exceed issues.
- Attractive UI
- User Authentication
- Simpler error details
- Personalized problem-solving question
- Thousands of validation for smooth work flow
Many features can be added, not actually this is how those coding platforms are created but yes some of the ground basics are similar.