-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch10.py
60 lines (47 loc) · 1.1 KB
/
ch10.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def myreplace(old, new, s):
"""
Replace all occurences of old with new in the string s.
>>> myreplace(',', ';', 'this, that, and, some, other, thing')
'this; that; and; some; other; thing'
>>> myreplace(' ', '**', 'Words will now be separated by stars.')
'Words**will**now**be**separated**by**stars.'
"""
return new.join(s.split(old))
def cleanword(word):
"""
>>> cleanword('what?')
'what'
>>> cleanword('"now!"')
'now'
>>> cleanword('?+="word!,@$()"')
'word'
"""
new_word = ''
sep = '?"!+=,@$()'
for x in word:
if x in sep:
continue
new_word += x
return new_word
def has_dashdash(s):
"""
>>> has_dashdash('distance--but')
True
>>> has_dashdash('several')
False
>>> has_dashdash('critters')
False
>>> has_dashdash('spoke--fancy')
True
>>> has_dashdash('yo-yo')
False
"""
sep = '--'
l = (s.split(sep))
if (len(l) > 1):
return True
else:
return False
if __name__ == '__main__':
import doctest
doctest.testmod()