Posts

6.00.1x - Week 5

Image
5.9.1 Object Oriented Programming Object:  3 pieces: type, internal data representation, and set of procedures for interaction with the object Instance is a particular type of object. 1234 is an instace of an int, a = 'hello' is an instance of a string Data abstraction:  represent internal data (private) using attributes, interface for interacting with object through methods Advantages of OOP:  bundle data into packages, divide-and-conquer development, and ability to reuse code 5.9.2 Class Instances class Coordinate(object): class: class definition class name: Coordinate class parent: object 5.9.3 Methods def __init__(self, attr): special method (constructor) to create an instance and initialize data (optional) __str__: equivalent to toString() in other languages to print custom string for class representational invariant: enforce a particular rule by code 5.9.5 Why OOP? 5.9.6 Hierarchies 5.9.7 Class Variables 5.9.9 Generators \

6.00.1x - Week 4

Image
4.7.1 Programming Challenges Check soup for bugs = Testing Keep lid closed =  Defensive Programming Clean kitchen = Eliminate source of bugs - debugging 4.7.2 Classes of Tests Defensive programming:   write specifications for function, modularize programs, check conditions on inputs/output (assertions) Write code to support easy testing and debugging, module, document constraints, document assumptions Classes of Tests:  Unit testing, Regression testing (rerun unit tests after bug fixing), and Integration testing (multiple unit testings). read 1 How to test:  Use intuition, random testing, black box testing (use specification without looking at code), or glass box testing (use/look at code) Path-complete glass box test:  find test cases that go through every possible path in the code. Glass box Recursion test cases: test recursion depth of 0, 1, and many While/for loop: loop not executing, executing 1, and executing more than 1 4.7.3 Bugs Do 3 things: isolate it, fix it

6.00.1x - Week 3

Image
3.5.1 Tuples ordered sequence of mixed element types immutable, represented with parentheses () must add comma ',' at the end of 1 element tuple, convenient for swapping variable values 3.5.2 Lists ordered sequence of mixed element types mutable, represented with square brackets [] https://docs.python.org/3/tutorial/datastructures.html 3.5.3 List Operations append, extend (change the list) del, pop, remove, list (convert string to list) sort vs. sorted, reverse range returns something that behaves like a tuple or a list 3.5.4 Mutation, Aliasing, Cloning clone: chill = old[:] sort: mutates the list, returns nothing sorted: does not mutate list, returns a new sorted list 3.5.5 Functions as Objects Can pass function name as parameter map(abs, [1, -2, -3, -4] => [1, 2, 3, 4] map(min, L1, L2) 3.6.1 Quick Review 3.6.2 Dictionaries instead of using multiple lists, use dictionary represented with curly braces: {} mutable, keys, values, no order key:

6.00.1x - Week 2

Image
2.3.1 Simple Algorithms slice string using: start:stop:step s[::-1] reverse the string string is immutable 2 ways to write for loop: "for char in s" "for index in range(len(s))" 2.3.2 Approximate Solutions Exhaustive enumeration start with initial guess step: any small number, takes many steps 2.3.3 Bisection Search takes log time to get an answer works well with problem with "ordering" property: value increases as input increases print("Hi", end=' ') print("there") will print "Hi there" 2.3.4 Floats and Fractions This is why sometimes we get weird result because in binary you can get close to it, but not exact Should not use == to compare float, instead use abs(a-b) < epsilon 2.3.5 Newton-Raphson Algorithm Approximation algorithm to find roots of a polynomial: g -> g - p(g)/p'(g) Even better than Bisection search

6.00.1x - Week 1

1.1 Introduction: Machine are good at calculations and remember results. Some problems require better algorithm to solve faster. Some problems are fundamentally hard:turing halting or decrypt encryption which is benefical for us. 1.1 Knowledge Declarative knowledge: statement of fact Imperative knowledge: recipe, how to solve Algorithm = sequence of steps + flow of control + when/how to stop 1.2 Machines Fixed program vs. Stored program Core of machine: Memory, Arithmetic Logic Unit (ALU), and Control Unit with program counter Interpreter vs Compiler: read 1 Turing machine: infinite tape, 6 primitives: move left, move right, scan, read, write, do nothing Anything computable in one language is computable in any other programming language 1.3 Languages Expression: legal combination of primitives Syntax: combination of primitives that makes sense, legal expression Static semantics: valid syntax that has meaning Semantic: meaning associated with a syntac

Conditional Statements

Image
Relational Operators Boolean Operators: and, or, and not Rule of precedence: Arithmetic > Relational > Boolean Short-circuit evaluation: Boolean expression involving 'and', if first operand evaluates to False, Python stops evaluation even if some operands have not been evaluated yet and the entire expression is considered to be False. Similarly for Boolean expression involving 'or' and one of the operands is True

Basic Machine Architecture

Image
Basic Machine Architecture includes: Memory: store/load instructions Arithmetic Logic Unit (ALU): perform primitive operations Control Unit: has a program counter to keep track of where things are and ask the ALU to perform operations. It is initially pointed to the first instruction Below are basic steps: Read in instruction, ie. interpreter translate codes into instruction steps and stores inside Memory Interpreter execute first instruction pointed by program counter in Control Unit First instruction: runs through ALU, perform primitive operations, then stores it back into Memory Program counter increases by 1, ie. go to the next instruction Every once in a while, one of the instruction is a Test. If Test is true, it will change the Program Counter which allows the machine to run to a different instruction (whether back or forward), ie. changing where we are in the code Reaches the point where we're done, then it will Output Turing showed that using 6 primit