-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4132a6
commit 98e5f16
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
https://cs50.harvard.edu/x/2023/psets/6/readability/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from cs50 import get_string | ||
|
||
|
||
def main(): | ||
text = get_string("Text: ") | ||
grade = coleman_liau(count_letters(text), count_words(text), count_sentences(text)) | ||
|
||
if grade < 1: | ||
print("Before Grade 1") | ||
elif grade >= 16: | ||
print("Grade 16+") | ||
else: | ||
print(f"Grade {grade}") | ||
|
||
|
||
def count_letters(text): | ||
count = 0 | ||
for char in text: | ||
if char.isalpha(): | ||
count += 1 | ||
return count | ||
|
||
|
||
def count_words(text): | ||
# Assumptions that a sentence: | ||
# - will contain at least one word; | ||
# - will not start or end with a space; and | ||
# - will not have multiple spaces in a row. | ||
|
||
count = 1 | ||
for char in text: | ||
if char == " ": | ||
count += 1 | ||
return count | ||
|
||
|
||
def count_sentences(text): | ||
# Assumptions that a sentence: | ||
# - is any sequence of characters that ends with a . or a ! or a ?; | ||
# Sentence boundary detection here is rudimentary | ||
|
||
count = 0 | ||
for char in text: | ||
if char in [".", "?", "!"]: | ||
count += 1 | ||
return count | ||
|
||
|
||
def coleman_liau(letter_count, word_count, sentence_count): | ||
L = letter_count / word_count * 100 | ||
S = sentence_count / word_count * 100 | ||
return round(0.0588 * L - 0.296 * S - 15.8) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
|