HackerRank Language Solution

Hello Programmers, In this post, you will know how to solve the HackerRank Language Solution. This problem is a part of the Regex HackerRank Series.

Ezoicreport this adHackerRank Language Solution
HackerRank Language Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Language Solution

Problem

Every submission at HackerRank has a field called language which indicates the programming language which a hacker used to code his solution.

C:CPP:JAVA:PYTHON:PERL:PHP:RUBY:CSHARP:HASKELL:CLOJURE:BASH:SCALA: ERLANG:CLISP:LUA:BRAINFUCK:JAVASCRIPT:GO:D:OCAML:R:PASCAL:SBCL:DART: GROOVY:OBJECTIVEC

Sometimes, errorprone API requests can have an invalid language field. Could you find out if a given submission has a valid language field or not?

Input Format

First line contains N. N API requests follow, each in a newline. Each request has an integer api_id and a string language which are the request parameters placed by the an API request.

Constraints

1 <= N <= 100
10^4 <= api_id < 10^5

a valid language is any of the languages listed above (case sensitive):

Output Format

For every api request given in input, print “VALID” if it has a valid language string in it or print “INVALID” otherwise.

Sample Input

3
11011 LUA
11022 BRAINFUCK
11044 X

Sample Output

VALID
VALID
INVALID

Explanation

LUA and BRAINFUCK are valid languages as listed above. As X is doesn’t appear in the list, it is an invalid request.

HackerRank Language Solutions in Cpp

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
#include<ctype.h>
using namespace std;
int main()
{
    long long N,num,count,C,L,a[100000],i,j,k,l,m,n,kase;
    string s[]={"C","CPP","JAVA","PYTHON",
    "PERL","PHP","RUBY","CSHARP","HASKELL","CLOJURE",
    "BASH","SCALA","ERLANG","CLISP","LUA","BRAINFUCK",
    "JAVASCRIPT","GO","D","OCAML","R","PASCAL",
    "SBCL","DART","GROOVY","OBJECTIVEC"};
    string s1;
    vector<long>v1;
    vector<long>v2;
    map<char,int>m1;
    map<char,int>m2;
    cin>>kase;
    count=0;
    while(kase--)
    {
        cin>>N;
        getchar();
        cin>>s1;
        bool chk=false;
        for(i=0;i<26;i++)
        {
            if(s[i]==s1)
            {
                chk=true;
                break;
            }
        }
        if(chk) cout<<"VALID"<<endl;
        else cout<<"INVALID"<<endl;
    }
        return 0;
    }

HackerRank Language Solutions in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        String languages="C:CPP:JAVA:PYTHON:PERL:PHP:RUBY:CSHARP:HASKELL:CLOJURE:BASH:SCALA:ERLANG:CLISP:LUA:BRAINFUCK:JAVASCRIPT:GO:D:OCAML:R:PASCAL:SBCL:DART:GROOVY:OBJECTIVEC";
        String[] language_array = languages.split(":");
        TreeSet<String> L = new TreeSet<String>();
        for(int i=0;i<language_array.length;i++)
            L.add(language_array[i]);
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for(int i=0;i<n;i++){
            int id = sc.nextInt();
            String lang = sc.next();
            if (id>=10000 && id <100000 && L.contains(lang))
                System.out.println("VALID");
            else
                System.out.println("INVALID");
        }
    }
}

HackerRank Language Solutions in Python

# Enter your code here. Read input from STDIN. Print output to STDOUT
import sys
l = ["C","CPP","JAVA","PYTHON","PERL","PHP","RUBY","CSHARP","HASKELL","CLOJURE","BASH","SCALA","ERLANG","CLISP","LUA","BRAINFUCK","JAVASCRIPT","GO","D","OCAML","R","PASCAL","SBCL","DART","GROOVY","OBJECTIVEC"]
n = int(input())
for i in range(n):
    [s1,s2]=raw_input().split()
    if(s2 not in l):
        sys.stdout.write('IN')
    sys.stdout.write('VALID\n')
sys.stdout.flush()
Ezoicreport this ad

HackerRank Language Solutions in JavaScript

'use strict';
function processData(input) {
    var parse_fun = function (s) { return parseInt(s, 10); };
    var apiRE = new RegExp(
          '^[0-9]{5} (?:'
        +  'C|CPP|JAVA|PYTHON|PERL|PHP|RUBY|CSHARP|HASKELL|CLOJURE|BASH|SCALA'
        + '|ERLANG|CLISP|LUA|BRAINFUCK|JAVASCRIPT|GO|D|OCAML|R|PASCAL|SBCL|DART'
        + '|GROOVY|OBJECTIVEC'
        + ')$'
    );
    var lines = input.split('\n');
    var N = parse_fun(lines.shift());
    var data = lines.splice(0, N);
    var res = 0;
    data.forEach(function (s) {
        console.log(((s.search(apiRE) == -1) ? 'INVALID' : 'VALID'));
    });
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
var _input = "";
process.stdin.on("data", function (input) { _input += input; });
process.stdin.on("end", function () { processData(_input); });

HackerRank Language Solutions in PHP

<?php
 function solve($query){
        //$pattern  = '/^[C|CPP|JAVA|PYTHON|PERL|PHP|RUBY|CSHARP|HASKELL|CLOJURE|BASH|SCALA|ERLANG|CLISP|LUA|BRAINFUCK|JAVASCRIPT|GO|D|OCAML|R|PASCAL|SBCL|DART|GROOVY|OBJECTIVEC]$/';
		$languages = array("C", "CPP", "JAVA", "PYTHON", "PERL", "PHP", "RUBY", "CSHARP", "HASKELL", "CLOJURE", "BASH", "SCALA", "ERLANG", "CLISP", "LUA", "BRAINFUCK", "JAVASCRIPT", "GO", "D", "OCAML", "R", "PASCAL", "SBCL", "DART", "GROOVY", "OBJECTIVEC");
		$response = "INVALID";
		$parts    = explode(" ", $query);
		$floor    = 10000;
		$ceiling  = 100000;
		$is_in_limits = false;
		$is_language  = false;
		$i = 0;
		
        if (count($parts) == 2){
			if (intval($parts[0]) >= $floor && intval($parts[0]) < $ceiling){
				$is_in_limits = true;
			} 
			while (!$is_language && $i < count($languages)){
				$is_language = strcmp($languages[$i], $parts[1]) == 0;
				$i = $i + 1;
			}
			if ($is_language && $is_in_limits){
				$response = "VALID";
			}
		}
		return $response;
    }    
    $_fp    = fopen("php://stdin", "r");
    
    $count   = intval(fgets( $_fp ));
    $i       = 0;
    
    while( $i < $count ) {
        $query = trim(fgets( $_fp ));
        fwrite(STDOUT, solve($query)."\n");
        $i = $i + 1;
    }
    fclose( $_fp );
?>

Disclaimer: This problem (HackerRank Language) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Split the Phone Numbers Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad