Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

CS50

Lecture 0

problem solving = ^^
inside the box is typically an algorithm

8 bits in a byte.

1 byte can represent 255 different values (remember this since it’s the reason RGB stores up to 255
too)

A = 65, B = 66 …

Lecture 1 - Arrays
Lecture 3 - Algorithms

Omega is the best-case equivalent of O()


Theta applies when the best and worst cases are of the same level i.e. Omega == O()
reversed random sorted

sort1 0.182 0.146 0.004

sort2 0.004 0.004 0.004

sort3 0.080 0.090 0.106

Lecture #3 - Memory

` char * is the type in C!


`

all it took though was typedef char *string;


` `
` malloc() and free()
` ` `
You don’t need to do ^^ manually, though. We have strcopy()
` `

` valgrind is for finding memory bugs/issues


`

We’ll use it to find:


forgetting to free()
` `

writing to memory that you shouldn’t be

Usage: valgrind ./app


` `

In C, you need to init values for yourself — else you have garbage values that can be anything!

Here’s how you do swap correctly:

usage: swap(&foo, &bar);


` `

basic implementation of getting an int from the user


Lecture #5 - Data Structures

Queues are FIFO (supporting enqueue and dequeue)


Stacks are LIFO (supporting push and pop)
a la a stack of trays in a cafeteria

Recall arrays store sets of data in contiguous memory.


Linked Lists
^^ Each pair of data and pointer is a node which can be defined in C with
Trees
Binary Search Trees
Dictionaries

Hashing
Taking a larger domain and reducing it to a smaller domain. Like taking a deck of cards and splitting it
into suits (before trying to sort the deck). Since you’re then taking a whole class of problems and
reducing it from 1x52 to 4x13. A hash function is used to do this.
` `
Hash Tables

Hash tables are an array of linked lists.


For those without a collision, you get a single-step retrieval. But those with a collision have x steps
` `

where x is the number of collisions. So you have a tradeoff decision to make based on the nature of
your data — either accept this tradeoff or make the initial array more granular (e.g., bucketing by first
letter → bucketing by first 2 letters) in exchange for using more memory.
** **

In the worst case (e.g., every name starts with H), a hash table is O(n) . But a well-designed hash
` `

table is O(n/k) where k is some constant, so it reduces back down to O(n) . But in practice, it can
` ` ` `

be many times faster than another O(n) structure like a linked list.
` `

Tries
Tries are an O(1) data structure.
` `

the green is used to convey the end of the key


Choosing a data structure

Usually start with thinking about the relative priority of:


insertion speed
search/retrieval speed
deletion speed
memory use (often no big deal these days)

E.g. a linked list is very efficient at insertion but quite poor at search

Lecture #6 - Python

```python

print("?" * 4) # => ????


```

```py

x = [1,2,3]
x += [4] # equivalent to x.append(4)
```
```py

x, y = y, x # swap values
```
Lecture #7 - SQL

```

$ sqlite3
sqlite> .mode csv
sqlite> .import favorites.csv favorites
sqlite> .schema
```

Can ease into joining by starting out with nested queries instead
```py

from cs50 import SQL


```

Lecture #8 - HTML, CSS, JS


Lecture #9 - Flask
Final Project Ideas
Today I Unlearned
with votes and ‘report to admins’?
idea: “similar lessons” using SQLite cosine stuff from Simon Willison
[ ]()

https://www.reddit.com/r/AskReddit/s/tQIbVauGfE
Survivor.ai
Spades AI
something something RAG

You might also like