Skip to content

Commit

Permalink
py imp
Browse files Browse the repository at this point in the history
  • Loading branch information
Azureki committed Apr 18, 2020
1 parent 9e48192 commit 4292e53
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 1410. HTML Entity Parser/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution:
def entityParser(self, text: str) -> str:
replace_dict={"&quot;":'"',"&apos;":"'","&amp;":"&","&gt;":">","&lt;":"<","&frasl;":"/"}
res = []
flag = False
keyword = []
for c in text:
if c == "&":
if flag:
res.append("".join(keyword))
flag = True
keyword = ["&"]
elif flag:
keyword.append(c)
if c == ";":
keyword = "".join(keyword)
res.append(replace_dict.get(keyword, keyword))
flag = False
else:
res.append(c)
if flag:
res.append("".join(keyword))
return "".join(res)
19 changes: 19 additions & 0 deletions 1410. HTML Entity Parser/readme.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

* Solution
其实可以用 replace 的,只要把 ~&amp;~ 另外判断就可以了。
#+BEGIN_SRC python
class Solution:
def entityParser(self, text: str) -> str:
special = {
"&quot;": '"',
"&apos;": "'",
# "&amp;": "&",
"&gt;": ">",
"&lt;": "<",
"&frasl;": "/"}

for entity, ch in special.items():
text = text.replace(entity, ch)
text = text.replace("&amp;", "&")
return text
#+END_SRC

0 comments on commit 4292e53

Please sign in to comment.