Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!