Learn Java proof at Hacker Rank in Tag Content Extractor

code_puzzleHi, some time ago I started learn Java language. I ma trying to self-study learn of the Java. And I have to say that the language is very nice and with deep knowledge of C# my progress is fast. I want to show you today here one of nice code puzzle solution named Tag Content Extractor. It take me some time. I started learn Java from very easy puzzles and every next one is more and more complicated. Also I am thinking about Java certification. However it is not a must, because I get the lesson that having certification in .NET even MCPD Enterprise Application Developer do not return me back any career opportunity, so I think certification is not very important. But maybe some basic step could be helpful. And I think that every good Software Architect should know at least C# and Java. I know that one of those languages could be the main and second well known only to understand software solutions better. Below you can find solution of mentioned puzzle. And if you are interested to learn Java with deep knowledge of C# you can go to this very nice how to. On the Hacker Rank I get so far from Java language trainings 248 points and 3199 rank :).

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) {
        Scanner in = new Scanner(System.in);
        int testCases = Integer.parseInt(in.nextLine());
        Pattern pattern = Pattern.compile(
            "<([/a-zA-Z]+).*?>"
        );
        while(testCases>0) {
            String line = in.nextLine();
            Matcher matcher = pattern.matcher(line);
            List<String> tags = new ArrayList<String>();
            while(matcher.find()) {
                String tag = matcher.group(0);
                tags.add(tag);
            }
            boolean none = true;
            while(tags.size() >= 2) {
                String tagB = tags.get(0);
                String tagE = tags.get(1);
                if (!tagB.replace("<", "</").equals(tagE)) {
                    tags.remove(0);
                    int x = line.indexOf(tagB);
                    line = line.substring(x + tagB.length());
                }
                else {
                    tags.remove(0);
                    tags.remove(0);
                    int x = line.indexOf(tagB) + tagB.length();
                    int y = line.indexOf(tagE, x);
                    String finding = line.substring(x,y);
                    line = line.substring(y + tagE.length());
                    if (!finding.isEmpty()) {
                        System.out.println(finding);
                        none = false;
                    }
                }
            }
            if (none) {
                System.out.println("None");
            }
            testCases--;
        }
    }
}

Thanks for reading and enjoy! It is still 50 lines and probably there is a way to use regex matcher to find out texts quicker, but this is the best I can do right now with my level of Java knowledge!

p ;).

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.