-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.py
More file actions
29 lines (22 loc) · 743 Bytes
/
stats.py
File metadata and controls
29 lines (22 loc) · 743 Bytes
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
def get_num_words(text):
return len(text.split())
def get_char_counts(text):
d = {}
for char in text.lower():
if not char.isalpha():
continue
if char in d:
d[char] += 1
else:
d[char] = 1
return d
def dict_to_list(dict_char_counts):
list_char_counts = []
for key, value in dict_char_counts.items():
list_char_counts.append({"name":key, "num":value})
# A function that takes a dictionary and returns the value of the "num" key
# This is how the `.sort()` method knows how to sort the list of dictionaries
def sort_on(items):
return items["num"]
list_char_counts.sort(reverse=True, key=sort_on)
return list_char_counts