HackerRank Language

Sort by

recency

|

104 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);
            int n = Integer.parseInt(scanner.nextLine());
            String str = "C:CPP:JAVA:PYTHON:PERL:PHP:RUBY:CSHARP:"; // The full string
            ArrayList<String> arraylist = new ArrayList<String>(Arrays.asList(str.split(":")));
            Pattern pattern = Pattern.compile("\\d+\\s*(.*)");
            for(int i=0;i<n;i++)
            {   Matcher matcher = pattern.matcher(scanner.nextLine());
                if (matcher.find())
                {   if (arraylist.contains(matcher.group(1)))
                        System.out.println("VALID");
                    else
                        System.out.println("INVALID");
                }
            }
        }
    }
    
  • + 0 comments

    java code

    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);
            Pattern pattern = Pattern.compile("\\d{5} (C|CPP|JAVA)"); // I couldn't put the entire string here due to the spam checker, but you get the point
            int n = Integer.parseInt(scanner.nextLine());
            for (int i = 0; i < n; i++) {
                Matcher matcher = pattern.matcher(scanner.nextLine());
                System.out.println((matcher.matches())? "VALID" : "INVALID");
            }
        }
    }
    
  • + 1 comment

    Python 3 solution. I was lazy to format the string into a regex :-))

    s = 'COPY THE FULL STRING FROM THE TEXT HERE, like: C:CPP...'
    
    valid_langs = set(lang.strip() for lang in s.split(':'))
    
    n = int(input())
    for _ in range(n):
        api_id, lang = input().split(' ')
        if pow(10, 4) <= int(api_id) < pow(10, 5) and lang in valid_langs:
            print('VALID')
        else:
            print('INVALID')
    
  • + 0 comments

    Python 3

    import re
    pattern = r'^\d{5}\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)$'
    for _ in range(int(input())):
        print('VALID' if bool(re.match(pattern, input())) else "INVALID")
    
  • + 0 comments
    import re
    s1 = "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"
    sl = re.split(r'\W',s1)
    sl = [i for i in sl if i!=""]
    
    for _ in range(int(input())):
        if any(map(lambda x : x in sl, input().split())) :
            print("VALID")
        else :
            print("INVALID")