Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Convert this code to python: import java.util.LinkedList; public class HtmlTags { public static boolean isHTMLMatched(String html) { LinkedList buffer = new LinkedList (); int j

Convert this code to python:

import java.util.LinkedList;

public class HtmlTags {

public static boolean isHTMLMatched(String html) {

LinkedList buffer = new LinkedList<>();

int j = html.indexOf('<');

while (j != -1) {

int k = html.indexOf('>', j + 1);

if (k == -1) {

return false;

}

String tag = html.substring(j + 1, k);

// This section will splits the tag and stores the tag name & attributes it contains.

tag = tag.trim();

String[] tagDetails = tag.split(" ");

for (String detail : tagDetails) {

if (!detail.startsWith("/")) {

buffer.push(detail);

} else {

if (buffer.isEmpty()) {

return false;

}

if (!tag.substring(1).equals(buffer.pop())) {

return false;

}

}

}

j = html.indexOf('<', k + 1);

}

//This section prints the tag detail to console.

System.out.println("You have " + buffer.getLast() + " tag which has following attributes");

for (int i = 0; i < buffer.size() - 1; i++) {

System.out.println(buffer.getFirst());

}

return buffer.isEmpty();

}

//this is test method. you can remove it or move it to some other class.

public static void main(String[] args) {

String html = "< table border=\"3\" cellpadding=\"5\" >";

isHTMLMatched(html);

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions