HackerRank Building a Smart IDE: Programming Language Detection Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Building a Smart IDE: Programming Language Detection Solution. This problem is a part of the Regex HackerRank Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the  Regex HackerRank Solutions using  CPP, JAVA, PYTHON, JavaScript & PHP Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

HackerRank Building a Smart IDE: Programming Language Detection Solution
HackerRank Building a Smart IDE: Programming Language Detection Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank Building a Smart IDE: Programming Language Detection Solution

Problem

We are trying to hack together a smart programming IDE. Help us build a feature which auto-detects the programming language, given the source code. There are only three languages which we are interested in “auto-detecting”: Java, C and Python.

We will provide you with links to a few short or medium size programs for Java, C and Python. In case you aren’t familiar with some of these languages, these samples will help you make observations about the lexical structure and syntax of these programming languages. These sample programs are only for your manual inspection. You cannot read or access these sample-programs from the code you submit.

After this, you will be provided with tests, where you are provided the source code for programs or partial code snippets, but you do not know which language they are in. For each test, try to detect which language the source code is in.

You might benefit from using regular expressions in trying to detect the lexical structure and syntax of the programs provided.

Sample Programs to Understand the Lexical Structure of different Programming Languages

Sample Programs and Code Snippets in C

Sample Programs and Code Snippets in Java

Sample Programs and Code Snippets in Python

INPUT FORMAT

Source code of a program, or a code snippet, which might be in C, Java or Python.

OUTPUT FORMAT

Just one line containing the name of the Programming language which you have detected: This might be either C or Java or Python.

SAMPLE INPUT

import java.io.*;

public class SquareNum {

public static void main(String args[]) throws IOException
{
System.out.println(“This is a small Java Program!);
}
}

SAMPLE OUTPUT

Java

HackerRank Building a Smart IDE: Programming Language Detection Solution in Cpp

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    string s;
    
    int a=0;
    bool b=true;
    for(int i=0;i<15;i++){
        getline(cin,s);
        a=s.find("java");
        if(a!=string::npos){
            cout<<"Java"<<endl;
            b=false;
            break;
        }
        a=s.find("stdio");
        if(a!=string::npos){
            cout<<"C"<<endl;
            b=false;
            break;
        }
    }
    if(b)cout<<"Python"<<endl;    return 0;
}

HackerRank Building a Smart IDE: Programming Language Detection Solution in Java

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		List<String> program = new ArrayList<String>();
		String line;

		try {
			while ((line = in.nextLine()) != null) {
				if (!line.equals("")) {
					program.add(line.trim());
				}
			}
		} catch (NoSuchElementException e) {

		}

		String imp = program.get(0);
		if ((imp.startsWith("package") || imp.startsWith("import"))
				&& imp.endsWith(";")) {
			System.out.println("Java");
			return;
		}

		if (imp.startsWith("#include")) {
			System.out.println("C");
			return;
		}
		boolean python = true;
		for (String s : program) {
			if (python && s.endsWith(";")) {
				python = false;
				break;
			}
		}
		if (python) {
			System.out.println("Python");
			return;
		}

		for (String s : program) {
			if (s.contains("class ") || s.contains("interface ")) {
				System.out.println("Java");
				return;
			}
		}

		System.out.println("C");
	}
}

HackerRank Building a Smart IDE: Programming Language Detection Solution in Python

import fileinput

stdin = [line.strip() for line in fileinput.input()]

tokens = []
for line in stdin:
  tokens.extend(line.split())

tokens = set(tokens)

if 'class' in tokens and '{' in tokens:
  print('Java')
elif '{' in tokens:
  print('C')
else:
  print('Python')

HackerRank Building a Smart IDE: Programming Language Detection Solution in JavaScript

function processData(input) {
    //Enter your code here
    if(checkPython(input)){
        console.log("Python");
        return;
    }
    if(checkJava(input)){
        console.log("Java");
        return;
    }
    if(checkC(input)){
        console.log("C");
        return;
    }
    console.log("C");
}

function checkPython(str){
    if(/\sdef\s[a-zA-Z0-9]*\(/g.test(str)){
        return true;
    }
    else if(/print\s/g.test(str)){
        return true;
    }else{
        return false;
    }
    
}

function checkJava(str){
    return /System\./g.test(str);
}

function checkC(str){
    if(/\s\*[a-zA-Z0-9]/g.test(str)){
        return true;
    }
    else if(/[a-zA-Z0-9]->[a-zA-Z0-9]/g.test(str)){
        return true;
    }else{
        return false;
    }
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

HackerRank Building a Smart IDE: Programming Language Detection Solution in PHP

<?php
    
    $python = true;
    for($i=0;$i<15;$i++){
        $line = trim(fgets(STDIN));
        $temp = strpos($line, "java");
        if ($temp!==false){
            $python=false;
            echo "Java";
            break;
        }
        $temp = strpos($line, "stdio");
        if ($temp!==false){
            $python=false;
            echo "C";
            break;
        }
    }
    if($python){
        echo "Python";
    }
 
?>

Disclaimer: This problem (Building a Smart IDE: Programming Language Detection) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

FAQ:

1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume. 

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite. 

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedia


Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment