Skip to content

Commit b198a49

Browse files
Merge pull request seeditsolution#263 from cadwellh5/patch-4
Stringpopulation_in_descending order
2 parents a7480c1 + bc33158 commit b198a49

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

stringPopulation

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from collections import defaultdict
2+
import string
3+
def extract_popular(textstring):
4+
"""
5+
Input: string
6+
Output: dictionary with counts for each character in a string
7+
"""
8+
d = defaultdict(int)
9+
exclude = set(string.punctuation)
10+
textstring = textstring.lower()
11+
textstring = ''.join(ch for ch in textstring if ch not in exclude)
12+
for c in textstring:
13+
d[c] += 1
14+
counts = [(d[c], c) for c in d]
15+
counts.sort()
16+
counts.reverse()
17+
return counts
18+
print(extract_popular("A man, a plan, a canal: Panama"))
19+
print(extract_popular("Testing, attention please"))

0 commit comments

Comments
 (0)