-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.py
More file actions
38 lines (30 loc) · 1.01 KB
/
stats.py
File metadata and controls
38 lines (30 loc) · 1.01 KB
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
29
30
31
32
33
34
35
36
37
38
# sort helper method
def sort_on(items):
return items["num"]
def get_word_count(book_contents):
book_words_list = book_contents.split()
number_of_words = len(book_words_list)
return number_of_words
def get_character_counts(book_contents):
characters_found = set()
character_counts = {}
for character in book_contents:
char_lower = character.lower()
if char_lower in characters_found:
character_counts[char_lower] += 1
else:
characters_found.add(char_lower)
character_counts[char_lower] = 1
return character_counts
def get_sorted_character_dict(character_counts):
sorted_character_counts = []
# create dictionaries
for key in character_counts:
this_character_count = {
"name": key,
"num": character_counts[key]
}
sorted_character_counts.append(this_character_count)
# sort
sorted_character_counts.sort(reverse=True, key=sort_on)
return sorted_character_counts