-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathrefactor.py
37 lines (29 loc) · 1.1 KB
/
refactor.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
# TODO: 1. Use one-liners
def count_asterisk_exceed_five(text):
return text.count("*") > 5
if __name__ == "__main__":
text = """Because he's the hero Gotham deserves but not the one it needs right now.
So we will hunt him, because he can take it. Because he's not out hero.
He is a silent guardian, a watchful protector... a dark knight."""
word1 = "hero"
word2 = "silent"
# TODO: 4. Consider general usage
# TODO: 3. Substitute algorithm
# TODO: 2. Extract method, remove duplicated code
# Censor `word1` from `text`
tmp = text
while word1 in tmp:
tmp = tmp[:tmp.find(word1)] + "*" * len(word1) + tmp[tmp.find(word1) + len(word1):]
text = tmp
if count_asterisk_exceed_five(text):
print("More than five *")
# TODO: 2. Extract method, remove duplicated code
# Censor `word2` from `text`
tmp = text
while word2 in tmp:
tmp = tmp[:tmp.find(word2)] + "*" * len(word2) + tmp[tmp.find(word2) + len(word2):]
text = tmp
if count_asterisk_exceed_five(text):
print("More than five *")
# Print result `text`
print(text)