Adding in character counting/reporting functionality

This commit is contained in:
franky212
2024-10-23 10:46:59 -06:00
parent 081d4deaa0
commit f0a53dbf74
2 changed files with 40 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
.venv
books/

38
main.py Normal file
View File

@@ -0,0 +1,38 @@
def main():
with open("./books/frankenstein.txt", 'r') as f:
file_contents = f.read()
count = count_words(file_contents)
print("--- Begin report of books/frankenstein.txt ---")
print(f"{count} words found in the document\n")
character_count(file_contents)
print("\n--- End report ---")
f.close()
def count_words(text):
words = text.split()
return len(words)
def character_count(text):
characters = {}
for word in text:
lowercased_word = word.lower()
for character in lowercased_word:
if character.isalpha():
if character in characters:
characters[character] += 1
else:
characters[character] = 1
character_list = []
for character in characters:
character_list.append({"character": character, "num": characters[character]})
character_list.sort(reverse=True, key=sort_on)
report(character_list)
def sort_on(dict):
return dict["num"]
def report(character_list):
for dict in character_list:
print(f"The '{dict["character"]}' character was found {dict["num"]}")
main()