Posts

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

Functions and Built-in Functions

Function: All functions start with reserved keyword 'def' All functions' definition ends with ':' By convention, indentation in Python is 4 spaces To write a comment, use '#' Every function returns a value. If there is no return statement, Python will return the special value None Note: print('hello ' + 'Huy') is the same as print('hello', 'Huy') def bye():     print('Goodbye') # print Goodbye and None

Variables, Expression and Assignment Statements

Image
Variable naming rule and convention: Can not start with a digit Uppercase and lowercase letters Digits Underscore '_' By convention, variable names are lowercase, separated by _ Variable names are case sensitive List of identifiers and reserved keywords:  https://docs.python.org/3/reference/lexical_analysis.html#identifiers Note above, since "area" was calculated before size change its value, area still 9 even though size is now 5 Syntax vs Semantic Errors: Above shows difference between syntax and semantic errors, though I really like this explanation. Expression vs Assignment Statement: Expression: evaluate, return/yield value/result Assignment: change state of variable/computaion and Python shell doesn't return anything Note: zipcode = '15245' zipcode + 3, Error because we can't add a type str and a type int zipcode * 3 will return 3 copies of zipcode, ie '152451524515245' z == (x * y...