CS50

Resources :

SLIDES >>>

Computer Science?

Computer Science is essentially Problem Solving: It’s defined as the study of how to represent and process information

The core idea is taking an input (the problem) and producing an output (the solution) through some form of processing Computer science is applicable to many fields, including arts, humanities, social sciences, and natural sciences


Represent Information

Base 2

  • Computers store and process information using binary, a base-two system with just two digits, 0 and 1
  • Physically, bits can be represented by electronic components like transistors being in one of two states, on or off
  • Numbers are represented using powers of 2, similar to how decimal (base 10) uses powers of 10
  • A byte is a common unit consisting of 8 bits, which allows for 256 possible values (from 0 to 255)
  • . Using binary is simpler for computers than systems with more states (like decimal) due to the ease of implementing clear on/off states with voltage

Letters and Text

  • Letters and text are represented by mapping characters to numbers
  • ASCII is an older standard that assigned numbers to English letters, numbers, and punctuation, using 7 or 8 bits

    01000001 == A

img

  • Unicode is a more modern standard that uses more bits (like 16, 24, or 32) to represent characters from virtually all human languages and symbols, including emojis, while being backwards compatible with ASCII
  • Emojis are stored as character numbers and displayed as pictures by the computer’s software

Colors

  • Colours are commonly represented using RGB (Red, Green, Blue) values
  • The colour of a single pixel (a point on the screen) can be defined by combining these values, often using three bytes (24 bits)
  • Images are composed of many pixels
  • Videos are essentially a series of images (frames) shown quickly, creating the illusion of motion
  • Sound can also be represented numerically, for example, by storing values related to frequency, volume, and duration

NOTE

Crucially, the same pattern of bits (or the corresponding number) can be interpreted differently depending on the context provided by the software. For example, the sequence of numbers 72, 73, 33 could represent the text “HI!” in a text editor, a specific colour (yellow) in an image editor, or numbers in a spreadsheet


Algorithms & Efficiency

  • An algorithm is a very precise, step-by-step description of how to perform a task or solve a problem
  • Different algorithms can solve the same problem, but some are much more efficient than others
  • A good algorithm can handle large problems much faster than a less efficient one

Here are three algorithms for finding a person’s phone number in a sorted phone book:

  1. Page by Page Search
    • Method: You start at the very beginning of the phone book and look at each page one after the other,You keep going until you find the name you’re looking for or you reach the very end of the book
    • Simplicity & Efficiency: This is a very simple method to understand and perform, However, it’s the least efficient of the three
  2. Skipping Pages
    • Method: Instead of looking at every page, you could try skipping pages, for example, looking at every second page (pages 2, 4, 6, etc.)
    • Simplicity & Efficiency: This is slightly less simple than the first method and, as initially described, had a bug because you could easily miss the page containing the name if it fell between the pages you checked, A fix would require potentially going back one page,It’s generally faster than the first method, taking roughly n/2 steps on average, plus potentially an extra step for the fix
  3. Divide and Conquer (Binary Search Concept)
    • Method: You start by opening the phone book roughly to the middle, You look at the names on that page and decide if the name you’re looking for is in the first half of the book (alphabetically before the names on the middle page) or the second half (alphabetically after the names on the middle page)
      • Once you’ve decided which half the name is in, you can effectively “tear” the problem in half, discarding the half that doesn’t contain the name
      • You then repeat this process with the remaining half: go to the middle of that section, look, and divide the problem in half again
      • You continue this process until you find the specific page you need or determine the name isn’t there
    • Simplicity & Efficiency: This algorithm is more complex to describe precisely,but it is significantly more efficient than the first two, By repeatedly dividing the problem in half, it takes far fewer steps for large phone books
    • For a phone book with 1000 pages, it would take roughly only 10 steps (because you can divide 1000 by 2 about 10 times)
    • If the phone book size doubles from 1000 to 2000 pages, this algorithm would only take about one additional step
    • This efficiency difference is visually represented by a graph where the time taken grows much, much slower as the problem size increases compared to the linear growth of the first two algorithms

Building Blocks of Programming

  • When translating algorithms into code (using languages like C or Python)
  • several fundamental constructs are used, as seen even in conceptual pseudocode
  • and visual languages like Scratch:
    • Functions: Named actions or tasks that perform a specific job. They can accept inputs (arguments) and can produce outputs (return values) or visible/audible results (side effects)
    • Conditionals: Allow the program to make choices. Examples include if, else if, and else statements
    • Boolean Expressions: Questions used within conditionals that have a true or false (yes/no, 1/0) answer
    • Loops: Allow a set of instructions to be repeated, such as repeat n times or forever
      • It’s important to design loops that terminate and avoid infinite loops unless intended
    • Variables: Placeholders used to store values that can be accessed and modified by the program
    • Abstraction: A powerful concept in programming where complex details are hidden away inside functions or modules, allowing programmers to use a concept (like “meow 3 times”) without needing to see or rewrite all the underlying steps

Artificial Intelligence (AI)

  • Modern AI systems, such as large language models used in chatbots like ChatGPT, are typically not programmed with explicit conditional responses for every possibility
  • Instead, they are trained on vast amounts of data and use statistics and probability to generate responses img
  • They are often implemented using neural networks

Debugging

  • Debugging: Making mistakes (bugs) is a normal part of programming
  • A recommended technique is “rubber duck debugging,” which involves explaining your code or problem out loud (even to an inanimate object like a rubber duck) to help identify the error
  • Learning Approach: The course encourages starting with small, manageable tasks (“baby steps”) and gradually adding complexity, It’s acknowledged that programming tasks often take longer than expected, The course emphasises that personal progress is what matters most, not how you compare to others