Cs50 Problem set 2 Readability Solution
Cs50 Problem set 2 Readability Solution |
from cs50 import get_string
# regex , regular expression
import re
# Defining main which will have all functions
def main():
# prompt the user for a text
text = get_string('Text: ')
words = get_words_count(text) # get_words_count will be defined under main()
letters = get_letters_count(text) / words # get_letters_count will be defined under main()
sentences = get_sentences_count(text) / words # get_sentences_count will be defined under main()
grading(letters, words, sentences)
def get_letters_count(text):
# [Capital and small letters from a to z]
letters = re.findall("[A-Za-z]", text)
letters_count = len(letters)
return letters_count
def get_words_count(text):
# Each space means there is a word
words = text.split(" ")
words_count = len(words)
return words_count
def get_sentences_count(text):
# each (. ? !) means an end of a sentence
indicators = re.findall("[.?!]", text)
sentences_count = len(indicators)
return sentences_count
def grading(L, W, S):
# Coleman-Liau index Rounded
grade = round(0.0588 * (L * 100) - 0.296 * (S * 100) - 15.8)
if grade >= 16:
print("Grade 16+")
elif grade < 1:
print("Before Grade 1")
else:
print(f"Grade {grade}")
main()
0 Comments
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.