in an html documentportions of text are delimited by html tags simple opening html tag has the form "and the corresponding closing tag has the form "for examplewe see the tag on the first line of figure ( )and the matching tag at the close of that document other commonly used html tags that are used in this example includebodydocument body section header centercenter justify pparagraph olnumbered (orderedlist lilist item ideallyan html document should have matching tagsalthough most browsers tolerate certain number of mismatching tags in code fragment we give python function that matches tags in string representing an html document we make left-to-right pass through the raw stringusing index to track our progress and the find method of the str class to locate the characters that define the tags opening tags are pushed onto the stackand matched against closing tags as they are popped from the stackjust as we did when matching delimiters in code fragment by similar analysisthis algorithm runs in (ntimewhere is the number of characters in the raw html source def is matched html(raw) """return true if all html tags are properly matchfalse otherwise "" arraystackfind first '<character (if any raw find while !- find next '>character raw findj+ if =- return false invalid tag tag raw[ + :kstrip away this is opening tag if not tag startswith) push(tag elsethis is closing tag if is empty) return false nothing to match with if tag[ :! pop) return false mismatched delimiter find next '<character (if any raw findk+ were all opening tags matched return is emptycode fragment function for testing if an html document has matching tags