HTML Parser – Part 2  Hacker Rank Solution – Queslers

Problem : HTML Parser – Part 2 Hacker Rank Solution

*This section assumes that you understand the basics discussed in HTML Parser – Part 1

.handle_comment(data)
This method is called when a comment is encountered (e.g. <!–comment–>).
The data argument is the content inside the comment tag:

from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_comment(self, data):
          print "Comment  :", data

.handle_data(data)
This method is called to process arbitrary data (e.g. text nodes and the content of <script>…</script> and <style>…</style>).
The data argument is the text content of HTML.

from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_data(self, data):
        print "Data     :", data

Task :

You are given an HTML code snippet of N lines.
Your task is to print the single-line comments, multi-line comments and the data.
Print the result in the following format:

>>> Single-line Comment  
Comment
>>> Data                 
My Data
>>> Multi-line Comment  
Comment_multiline[0]
Comment_multiline[1]
>>> Data
My Data
>>> Single-line Comment:  

Note: Do not print data if data == ‘\n’.


Input Format :

The first line contains integer N, the number of lines in the HTML code snippet.
The next N lines contains HTML code.

Constraints :

  • 0 < N < 100

Output Format :

Print the single-line comments, multi-line comments and the data in order of their occurrence from top to bottom in the snippet.
Format the answers as explained in the problem statement.


Sample Input :

4
<!--[if IE 9]>IE9-specific content
<![endif]-->
<div> Welcome to HackerRank</div>
<!--[if IE 9]>IE9-specific content<![endif]-->

Sample Output :

>>> Multi-line Comment
[if IE 9]>IE9-specific content
<![endif]
>>> Data
 Welcome to HackerRank
>>> Single-line Comment
[if IE 9]>IE9-specific content<![endif]

HTML Parser – Part 2 Hacker Rank Solution in python 2

from HTMLParser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_comment(self, data):
        if "\n" in data:
            print ">>> Multi-line Comment"
        else:
            print ">>> Single-line Comment"
        print data
    def handle_data(self, data):
        if len(data.strip())>0:
            print ">>> Data"
            print data

html = ""       
for i in range(int(raw_input())):
    html += raw_input().rstrip()
    html += '\n'
    
parser = MyHTMLParser()
parser.feed(html)
parser.close()

HTML Parser – Part 2 Hacker Rank Solution in python 3

from html.parser import HTMLParser
html = ""
class MyHTMLParser(HTMLParser):
    def handle_comment(self,data):
        if('\n' in data):
            print(">>> Multi-line Comment")
        else:
            print(">>> Single-line Comment")
        print(data)
    def handle_data(self,data):
        if(data != '\n'):
            print(">>> Data")
            print(data)
             
for i in range(int(input())):
    html += input().rstrip()
    html += '\n'
#print(html)    
parser = MyHTMLParser()
parser.feed(html)
parser.close()

HTML Parser – Part 2 Hacker Rank Solution in pypy

from HTMLParser import HTMLParser
class CustomHTMLParser(HTMLParser):
    def handle_comment(self, data):
        number_of_line = len(data.split('\n'))
        if number_of_line>1:
            print('>>> Multi-line Comment')
        else:
            print('>>> Single-line Comment')
        if data.strip():
            print(data)

    def handle_data(self, data):
        if data.strip():
            print(">>> Data")
            print(data)

parser = CustomHTMLParser()

n = int(input())

html_string = ''
for i in range(n):
    html_string += raw_input().rstrip()+'\n'
    
parser.feed(html_string)
parser.close()

HTML Parser – Part 2 Hacker Rank Solution in pypy 3

# Enter your code here. Read input from STDIN. Print output to STDOUT
from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
    def handle_comment(self, data):
        lines = data.split("\r")
        if len(lines) > 1:
            print(">>> Multi-line Comment")
            for l in lines:
                print(l)
        else:
            print(">>> Single-line Comment")
            print(lines[0])

    def handle_data(self, data):
        if len(data.strip()) == 0:
            return
        print(">>> Data")
        lines = data.split("\r")
        for l in lines:
            if l.strip() != "":
                print(l)

n = int(input())
input_data =""
for _ in range(n):
    input_data += input()
parser = MyHTMLParser()
parser.feed(input_data)
HTML Parser – Part 2 Hacker Rank Solution Review:

In our experience, we suggest you solve this HTML Parser – Part 2 Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

HTML Parser – Part 2 is available on Hacker Rank for Free, if you are stuck anywhere between compilation, just visit Queslers to get all Hacker Rank Solution

HTML Parser – Part 2 Hacker Rank Solution Conclusion:

I hope this HTML Parser – Part 2 Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Mini-Max Sum Hacker Rank Solution

String Validators Hacker Rank Solution

Text Alignment Hacker Rank solution

String validators Hacker Rank Solution

Staircase Hacker Rank Solution

Leave a Reply

Your email address will not be published.