Grokking Algorithms Audiobook
Grokking Algorithms Audiobook
CHAPTER 1
LOGARITHMS
You may not remember what logarithms are, but you probably know
what exponentials are. log10 100 is like asking, “How many 10s do we
multiply together to get 100?” The answer is 2: 10 × 10. So log10 100 =
2. Logs are the ip of exponentials.
1 low = 0
2 high = len(list) - 1
1 if guess < item:
2 low = mid + 1
CHAPTER 2
CHAPTER 3
1 def look_for_key(main_box):
2 pile = main_box.make_a_pile_to_look_through()
3 while pile is not empty:
4 box = pile.grab_a_box()
5 for item in box:
6 if item.is_a_box():
7 pile.append(item)
8 elif item.is_a_key():
9 print "found the key!"
1 > 3...2...1
1 def countdown(i):
2 print i
3 countdown(i-1)
1 > 3...2...1...0...-1...-2...
1 def greet(name):
2 print "hello, " + name + "!"
3 greet2(name)
4 print "getting ready to say bye..."
5 bye()
1 def greet2(name):
2 print "how are you, " + name + "?"
3
4 def bye():
5 print "ok bye!"
1 def fact(x):
2 if x == 1:
3 return 1
4 else:
5 return x * fact(x-1)
CHAPTER 4
1 def sum(arr):
2 total = 0
3 for x in arr:
4 total += x
5 return total
6
7 print sum([1, 2, 3, 4])
SNEAK PEAK AT FUNCTIONAL PROGRAMMING
Notice that it looks like you have two de nitions for the function. The
rst de nition is run when you hit the base case. The second de nition
runs at the recursive case. You can also write this function in Haskell
using an if statement:
1 >>> check_voter("tom")
2 let them vote!
3 >>> check_voter("mike")
4 let them vote!
5 >>> check_voter("mike")
6 kick them out!
1 graph["anuj"] = []
2 graph["claire"] = ["thom", "jonny"]
1 def person_is_seller(name):
2 return name[-1] == 'm'
CHAPTER 7
1 graph = {}
1 graph["start"] = {}
2 graph["start"]["a"] = 6
3 graph["start"]["b"] = 2
1 >>> print graph["start"].keys()
2 ["a", "b"]
1 infinity = float("inf")
2 costs = {}
3 costs["a"] = 6
4 costs["b"] = 2
5 costs["fin"] = infinity
1 parents = {}
2 parents["a"] = "start"
3 parents["b"] = "start"
4 parents["fin"] = None
1 processed = []
CHAPTER 8
1 >>> arr = [1, 2, 2, 3, 3, 3]
1 >>> set(arr)
2 set([1, 2, 3])
1 stations = {}
2 stations["kone"] = set(["id", "nv", "ut"])
3 stations["ktwo"] = set(["wa", "id", "mt"])
4 stations["kthree"] = set(["or", "nv", "ca"])
5 stations["kfour"] = set(["nv", "ut"])
6 stations["kfive"] = set(["ca", "az"])
1 final_stations = set()
1 best_station = None
2 states_covered = set()
3 for station, states_for_station in stations.items():
1 covered = states_needed & states_for_station
1 covered = states_needed & states_for_station
1 states_needed -= states_covered
1 while states_needed:
2 best_station = None
3 states_covered = set()
4 for station, states in stations.items():
5 covered = states_needed & states
6 if len(covered) > len(states_covered):
7 best_station = station
8 states_covered = covered
9
10 states_needed -= states_covered
11 final_stations.add(best_station)
Here’s how I would do it: arbitrarily pick a start city. Then, each time
the salesperson has to pick the next city to visit, they pick the closest
unvisited city. Suppose they start in Marin.
Total distance: 71 miles. Maybe it’s not the shortest path, but it’s still
pretty short.
CHAPTER 9
What happens if you add an item?
CHAPTER 10
Picking good features
CHAPTER 11
1 >>> arr1 = [1, 2, 3, 4, 5]
2 >>> arr2 = map(lambda x: 2 * x, arr1)
3 [2, 4, 6, 8, 10]
1 >>> arr1 = # A list of URLs
2 >>> arr2 = map(download_page, arr1)