Posts

Showing posts with the label algorithm

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