Skip to content

Commit 0383068

Browse files
author
boraxpr
committed
bite 106
1 parent 328d884 commit 0383068

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

106/test_vowels.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from vowels import strip_vowels, text
2+
3+
4+
def test_strip_vowels_on_zen():
5+
output, number_replacements = strip_vowels(text)
6+
7+
assert number_replacements == 262
8+
9+
assert 'Th* Z*n *f Pyth*n, by T*m P*t*rs' in output
10+
assert 'B***t*f*l *s b*tt*r th*n *gly' in output
11+
assert 'N*m*sp*c*s *r* *n* h*nk*ng gr**t *d**' in output
12+
assert '*lth**gh pr*ct*c*l*ty b**ts p*r*ty.' in output
13+
14+
15+
def test_strip_vowels_on_other_text():
16+
text = """Hello world!
17+
We hope that you are learning a lot of Python.
18+
Have fun with our Bites of Py.
19+
Keep calm and code in Python!
20+
Become a PyBites ninja!
21+
All the way"""
22+
23+
output, number_replacements = strip_vowels(text)
24+
25+
assert number_replacements == 46
26+
27+
assert 'H*ll* w*rld!' in output
28+
assert 'H*v* f*n w*th **r B*t*s *f Py' in output
29+
assert 'B*c*m* * PyB*t*s n*nj*!' in output
30+
assert '*ll th* w*y' in output
31+
32+
33+
# test_strip_vowels_on_zen()
34+
# test_strip_vowels_on_other_text()

106/vowels.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
text = """
2+
The Zen of Python, by Tim Peters
3+
4+
Beautiful is better than ugly.
5+
Explicit is better than implicit.
6+
Simple is better than complex.
7+
Complex is better than complicated.
8+
Flat is better than nested.
9+
Sparse is better than dense.
10+
Readability counts.
11+
Special cases aren't special enough to break the rules.
12+
Although practicality beats purity.
13+
Errors should never pass silently.
14+
Unless explicitly silenced.
15+
In the face of ambiguity, refuse the temptation to guess.
16+
There should be one-- and preferably only one --obvious way to do it.
17+
Although that way may not be obvious at first unless you're Dutch.
18+
Now is better than never.
19+
Although never is often better than *right* now.
20+
If the implementation is hard to explain, it's a bad idea.
21+
If the implementation is easy to explain, it may be a good idea.
22+
Namespaces are one honking great idea -- let's do more of those!
23+
"""
24+
vowels = 'aeiou'
25+
26+
27+
def strip_vowels(text: str) -> (str, int):
28+
"""Replace all vowels in the input text string by a star
29+
character (*).
30+
Return a tuple of (replaced_text, number_of_vowels_found)
31+
32+
So if this function is called like:
33+
strip_vowels('hello world')
34+
35+
... it would return:
36+
('h*ll* w*rld', 3)
37+
38+
The str/int types in the function defintion above are part
39+
of Python's new type hinting:
40+
https://docs.python.org/3/library/typing.html"""
41+
listkun = list(text)
42+
count = 0
43+
for x in listkun:
44+
if x in vowels or x in "AEIOU":
45+
count+=1
46+
47+
text = text.replace('a', '*')
48+
text = text.replace('e', '*')
49+
text = text.replace('i', '*')
50+
text = text.replace('o', '*')
51+
text = text.replace('u', '*')
52+
text = text.replace('A', '*')
53+
text = text.replace('E', '*')
54+
text = text.replace('I', '*')
55+
text = text.replace('O', '*')
56+
text = text.replace('U', '*')
57+
58+
return (text, count)
59+
60+
# print(strip_vowels(text))
61+
print(strip_vowels(text))

0 commit comments

Comments
 (0)