Featured Posts

CS50 Python , Nutrition Facts Table

Image
Nutrition Facts: Python Practice for Beginners Nutrition Facts Table for Python Practice Welcome to this comprehensive guide for Python beginners! If you are learning how to work with lists, dictionaries, and loops, this post will help you build practical skills using a real-world example: nutrition facts for fruits. Understanding how to organize and manipulate data is a key part of programming, and this exercise will give you hands-on experience. Below is a sample table of fruits and their calorie values, formatted as a Python list of dictionaries. This structure is ideal for coding exercises, projects, or even building your own nutrition calculator. You can expand this list, add new fruits, or use it as a foundation for more advanced Python tasks. Python List of Dictionaries Example: fruits = [ {'name': 'Apple', 'calories': 130}, {'name': 'Avocado', 'calories': 50}, {'name': 'Banana', 'ca...

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

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