forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHTML Entity Parser.py
More file actions
37 lines (28 loc) · 1.19 KB
/
HTML Entity Parser.py
File metadata and controls
37 lines (28 loc) · 1.19 KB
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
class Solution:
def entityParser(self, text: str) -> str:
d = {""" : '"' , "'":"'" , "&" : "&" , ">" : ">" , "<":"<" , "⁄" : "/"}
ans = ""
i = 0
while i < len(text):
bag = ""
#condition if find & and next char is not & also and handdling index out of range for i + 1
if i+1 < len(text) and text[i] == "&" and text[i+1] != "&":
#create subtring for speacial char till ";"
for j in range(i , len(text)):
if text[j] == ";":
bag += text[j]
break
else:
bag += text[j]
#if that not present in dict we added same as it is
if bag not in d:
ans += bag
else:
ans += d[bag]
#increment by length of bag
i += len(bag)
#otherwise increment by 1
else:
ans += text[i]
i += 1
return ans