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...

cs50 Week 2 Problem Set 2 Readability Solution

CS50 Week 2 Problem Set 2 Readability

Cs50 Problem set 2 Readability Solution
Problem Set 2 - Readability

In coding, problems have different ways to get to the solution. Below code is one way. Soon, we will be posting different codes.

** Remember:

Try to change some things in the code before submitting it in order to avoid being rejected as Harvard academic honesty.

The Solution:

#include <cs50.h>

#include <stdio.h>

#include <string.h>

#include <math.h>

#include <ctype.h>

int main(void)

{

    // inintiate needed variables

    string text = get_string("TEXT: ");

    int letters = 0;

    int words = 1;

    int sentences = 0;

    for (int i = 0; i < strlen(text); i++)

    {

        if (isalpha(text[i]))

        {

            letters++;

        }

        else if (text[i] == ' ')

        {

            words++;

        }

        else if (text[i] == '.' || text[i] == '?' || text[i] == '!')

        {

            sentences++;

        }

    }


    // Initiate variables needed to construct the formula

    float L = (float) letters / (float) words * 100;

    float S = (float) sentences / (float) words * 100;

    int index = round(0.0588 * L - 0.296 * S - 15.8);


    if (index < 1)

    {

        printf("Before Grade 1\n");

    }

    else if (index > 16)

    {

        printf("Grade 16+\n");

    }

    else

    {

        printf("Grade %i\n" , index);

    }

}




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