Building a Smart IDE: Programming Language Detection Discussions | Regex | HackerRank

Building a Smart IDE: Programming Language Detection

Sort by

recency

|

94 Discussions

|

  • + 0 comments

    Java 15

    import java.io.*;
    import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String output="";
            Pattern c_pattern = Pattern.compile("#include<stdio.h>");
            Pattern java_pattern = Pattern.compile("import java");
            Pattern python_pattern = Pattern.compile("(^print|def )");
            while(scanner.hasNextLine()){
                String line = scanner.nextLine();
                if (c_pattern.matcher(line).find()) output="C";
                else  if (java_pattern.matcher(line).find()) output="Java";
                else  if (python_pattern.matcher(line).find()) output="Python";
                if(!output.equals(""))  break;
            }
            System.out.println(output);
        }
    }
    
  • + 0 comments
    import re
    import sys
    
    content = sys.stdin.read()
    
    if re.match(r'.*?class [^{]+{', content, re.DOTALL):
        print('Java')
    elif re.match(r'.*?#include<', content, re.DOTALL):
        print('C')
    else:
        print('Python')
    
  • + 0 comments
    import re
    import sys
    
    code = sys.stdin.read()
    
    if re.search("#include<stdio.h>", code) is not None:
        print("C")
    elif re.search("import java.io", code) is not None:
        print("Java")
    else:
        print("Python")
    
  • + 0 comments

    Python 3

    import re
    import sys
    string = sys.stdin.read()
    if bool(re.findall(r'\bdef\b|[^\.]print\b', string)):
        print("Python")
    elif bool(re.findall(r"\bpublic\s+class\b|\bpublic\s+static\s+void\s+main\b|\bSystem\.out\.println\b", string)):
        print('Java')
    elif bool(re.findall(r'\b#include\b|\bint\s+main\b|\bprintf\b', string)):
        print('C')
    
  • + 0 comments
    import sys
    m=sys.stdin.read()
    if "#include" in m:
        print("C")
    elif "java" in m:
        print("Java")
    else:
        print("Python")