CS50X readability

Cs50 Problem set 2 Readability Solution

Cs50 Problem set 2 Readability Solution
Cs50 Problem set 2 Readability Solution

The Code 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()

Comments

Popular posts from this blog

فرصتك للدراسة في ألمانيا: منحة ممولة بالكامل لطلاب الدراسات العليا

How to Deactivate Screen Reader in Kali Linux

Murphy's Law: Expect the Unexpected

Python 3.2.1.14 LAB: Essentials of the while loop

7 Reasons Why You Need to Learn English

Data Analysis Roadmap 2026: From Excel Lover to Python-Powered Analyst

15 Python Builtin-Functions You Need to Master as a Beginner

Prefixes vs Suffixes ? What are they?

Python 4.3.1.7 LAB: How many days: writing and using your own functions

Python 4.3.1.6 LAB: A leap year: writing your own functions