| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 file changed
| Original file line number | Diff line number | Diff 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")) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments