-
Notifications
You must be signed in to change notification settings - Fork 44
/
htmlElements.py
40 lines (28 loc) · 1.54 KB
/
htmlElements.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
''' Have the function HTMLElements(str) read the str parameter being passed which will be a string of HTML DOM elements and plain text.
The elements that will be used are: b, i, em, div, p. For example: if str is "<div><b><p>hello world</p></b></div>" then this string of
DOM elements is nested correctly so your program should return the string true.
If a string is not nested correctly, return the first element encountered where, if changed into a different element, would result in a
properly formatted string. If the string is not formatted properly, then it will only be one element that needs to be changed. For example:
if str is "<div><i>hello</i>world</b>" then your program should return the string div because if the first <div> element were changed into
a <b>, the string would be properly formatted.
Examples
Input: "<div><div><b></b></div></p>"
Output: div
Input: "<div>abc</div><p><em><i>test test test</b></em></p>" '''
import re
def HTMLElements(strParam):
open_tags = ['<b>', '<i>', '<em>', '<div>', '<p>']
close_tags = ['</b>', '</i>', '</em>', '</div>', '</p>']
stack = []
tags = re.split('(<[^>]*>)', strParam)
for tag in tags:
if tag in open_tags:
stack.append(tag)
elif tag in close_tags:
check = close_tags.index(tag)
if (len(stack) > 0) and (open_tags[check] == stack[len(stack)-1]):
stack.pop()
if stack:
return stack[-1].replace('<', '').replace('>', '')
return True
print(HTMLElements(input()))