80% found this document useful (10 votes)
3K views147 pages

Grokking Algorithms Audiobook

The document provides examples and explanations of logarithms, binary search, and recursion. It includes code snippets for functions using recursion to calculate factorials and sums. It also introduces mapping and reducing as functional programming concepts. Later sections discuss graphs and graph algorithms, hashing, dynamic programming, and machine learning topics like feature selection. Code shows implementing dictionaries, sets, graphs, and algorithms like shortest path finding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
80% found this document useful (10 votes)
3K views147 pages

Grokking Algorithms Audiobook

The document provides examples and explanations of logarithms, binary search, and recursion. It includes code snippets for functions using recursion to calculate factorials and sums. It also introduces mapping and reducing as functional programming concepts. Later sections discuss graphs and graph algorithms, hashing, dynamic programming, and machine learning topics like feature selection. Code shows implementing dictionaries, sets, graphs, and algorithms like shortest path finding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 147

CODE LISTINGS AND FIGURES

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.

Logs are the ip of exponentials.

In this book, when I talk about running time in Big O notation


(explained a little later), log always means log2. When you search for
an element using simple search, in the worst case you might have to
look at every single element. So for a list of 8 numbers, you’d have to
check 8 numbers at most. For binary search, you have to check log n
elements in the worst case. For a list of 8 elements, log 8 == 3, because
2 3 == 8. So for a list of 8 numbers, you would have to check 3 numbers
at most. For a list of 1,024 elements, log 1,024 = 10, because 2 10 ==
1,024. So for a list of 1,024 numbers, you’d have to check 10 numbers at
most.

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

“Why would I do this recursively if I can do it easily with a loop?” you


may be thinking. Well, this is a sneak peek into functional
programming! Functional programming languages like Haskell don’t
have loops, so you have to use recursion to write functions like this. If
you have a good understanding of recursion, functional languages will
be easier to learn. For example, here’s how you’d write a sum function
in Haskell:

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 sum arr = if arr == []


2 then 0
3 else (head arr) + (sum (tail arr))
But the rst de nition is easier to read. Because Haskell makes heavy
use of recursion, it includes all kinds of niceties like this to make
recursion easy. If you like recursion, or you’re interested in learning a
new language, check out Haskell.
1 def quicksort(array):
2 if len(array) < 2:
3 return array
1 def print_items(list):
2 for item in list:
3 print item

1 from time import sleep


2 def print_items2(list):
3 for item in list:
4 sleep(1)
5 print item
CHAPTER 5
1 >>> book = dict()
1 >>> phone_book = dict()

1 >>> phone_book["jenny"] = 8675309


2 >>> phone_book["emergency"] = 911
Preventing duplicate entries
1 >>> voted = {}

1 >>> value = voted.get("tom")


1 voted = {}
2
3 def check_voter(name):
4 if voted.get(name):
5 print "kick them out!"
6 else:
7 voted[name] = True
8 print "let them vote!"

1 >>> check_voter("tom")
2 let them vote!
3 >>> check_voter("mike")
4 let them vote!
5 >>> check_voter("mike")
6 kick them out!

Using hash tables as a cache


CHAPTER 6
Queues
1 graph = {}
2 graph["you"] = ["alice", "bob", "claire"]
1 graph = {}
2 graph["you"] = ["alice", "bob", "claire"]
3 graph["bob"] = ["anuj", "peggy"]
4 graph["alice"] = ["peggy"]
5 graph["claire"] = ["thom", "jonny"]
6 graph["anuj"] = []
7 graph["peggy"] = []
8 graph["thom"] = []
9 graph["jonny"] = []

1 graph["claire"] = ["thom", "jonny"]


2 graph["anuj"] = []

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["you"] = ["alice", "bob", "claire"]

1 graph["start"] = {}
2 graph["start"]["a"] = 6
3 graph["start"]["b"] = 2
1 >>> print graph["start"].keys()
2 ["a", "b"]

1 >>> print graph["start"]["a"]


2 2
3 >>> print graph["start"]["b"]
4 6
1 infinity = float("inf")

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 if len(covered) > len(states_covered):


2 best_station = station
3 states_covered = covered
1 final_stations.add(best_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)

1 >>> print final_stations


2 set(['ktwo', 'kthree', 'kone', 'kfive'])
APPROXIMATING

What’s a good approximation algorithm for the traveling salesperson?


Something simple that nds a short path. See if you can come up with
an answer before reading on.

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)

1 >>> arr1 = [1, 2, 3, 4, 5]


2 >>> reduce(lambda x,y: x+y, arr1)
3 15

You might also like