Skip to content

Commit 570476d

Browse files
committed
add count_vowel plalindrome reverse_string
1 parent 2dc37b0 commit 570476d

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__pycache__/
2+
*.py[cod]
3+
.pytest_cache/
4+
.mypy_cache/

text-operation/count_vowel.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
text-operation.count_vowel
5+
--------------------------
6+
7+
统计元音字母——输入一个字符串,统计处其中元音字母的数量。更复杂点的话统计出每个元音字母的数量。
8+
:copyright: (c) 2018-09-15 by buglan
9+
"""
10+
keys = {'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}
11+
12+
13+
def count_vowel(string: str) -> dict:
14+
for s in string:
15+
if s in keys:
16+
keys[s] += 1
17+
return keys
18+
19+
20+
def test_count_vowel():
21+
string = 'aeiiousdfsdfsdf'
22+
assert count_vowel(string) == {'a': 1, 'e': 1, 'i': 2, 'o': 1, 'u': 1}

text-operation/plalindrome.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
text-operation.plalindrome
5+
--------------------------
6+
7+
判断是否为回文——判断用户输入的字符串是否为回文。回文是指正反拼写形式都是一样的词,譬如“racecar”
8+
:copyright: (c) 2018-09-15 by buglan
9+
"""
10+
from typing import List, Optional, Any
11+
12+
13+
class FullError(Exception):
14+
pass
15+
16+
17+
class EmptyError(Exception):
18+
pass
19+
20+
21+
class Stack:
22+
def __init__(self, maxcount: Optional[int] = None) -> None:
23+
self.items: List = []
24+
self.maxcount = maxcount
25+
if self.maxcount is not None and self.maxcount <= 0:
26+
raise Exception("self.maxcount cann't <= 0")
27+
28+
def __len__(self) -> int:
29+
return len(self.items)
30+
31+
def push(self, value: Any) -> None:
32+
if self.maxcount is not None and len(self) >= self.maxcount:
33+
raise FullError(f"len(self) >= {self.maxcount}")
34+
self.items.append(value)
35+
36+
def pop(self) -> Any:
37+
if len(self) <= 0:
38+
raise EmptyError("len(self) <= 0")
39+
return self.items.pop()
40+
41+
def is_empty(self) -> bool:
42+
return len(self.items) == 0
43+
44+
def peak(self) -> Any:
45+
if len(self) <= 0:
46+
raise EmptyError("len(self) <= 0")
47+
return self.items[-1]
48+
49+
50+
def is_plalindrome(string: str) -> bool:
51+
length: int = len(string)
52+
stack: 'Stack' = Stack(length // 2)
53+
for i in range(length // 2):
54+
stack.push(string[i])
55+
s: str = ""
56+
for i in range(length // 2):
57+
s += stack.pop()
58+
if len(string) // 2 == 0 and s == string[length // 2:]:
59+
return True
60+
elif s == string[length // 2 + 1:]:
61+
return True
62+
return False
63+
64+
65+
def test_is_plalindrome():
66+
string = 'racecar'
67+
assert is_plalindrome(string)
68+
string = '妈妈爱我,我爱妈妈'
69+
assert is_plalindrome(string)
70+
string = 'aabb'
71+
assert not is_plalindrome(string)
72+
string = 'abba'
73+
assert not is_plalindrome(string)

text-operation/reverse_string.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
"""
4+
text-operation.reverse_string
5+
-----------------------------
6+
7+
反转字符串
8+
:copyright: (c) 2018-09-13 by buglan
9+
"""
10+
from typing import List
11+
12+
13+
def reverse_string(string: str) -> str:
14+
return string[::-1]
15+
16+
17+
def reverse_string2(string: str) -> str:
18+
"""
19+
string is unmut type
20+
so cann't use string[i] = ...
21+
"""
22+
length: int = len(string)
23+
strings: List[str] = list(string)
24+
for i in range(length // 2):
25+
strings[i], strings[length - i - 1] = strings[length - i -
26+
1], strings[i]
27+
return "".join(strings)
28+
29+
30+
def test_reverse_string():
31+
string = "abcde"
32+
assert "edcba" == reverse_string(string)
33+
34+
35+
def test_reverse_string2():
36+
string = "abcde"
37+
assert "edcba" == reverse_string2(string)

0 commit comments

Comments
 (0)