Algorithms
Everything in Learn Python was about getting the right answer. The practice problems accepted any solution that worked, however slowly. This section is about the next question: how much work does the code do, and what to do when the answer is "too much".
The pages here are shorter than the earlier ones and expect you to be comfortable with lists, dictionaries, loops and functions. If you are not, go back and do the practice problems first; nothing here will make sense without them.
- Counting steps: measure what code costs and name the common growth shapes.
- Recursion: functions that call themselves, and the problems that need them.
- Searching: binary search, its boundaries, and searching on an answer.
- Sorting: why the simple sorts are slow, merge sort, and getting the most from
sorted. - Hashing: why dict and set are fast, and the patterns they enable.
- Two pointers, windows and prefix sums: one pass instead of a nested loop.
- Linked lists: nodes, relinking, reversal, fast and slow pointers.
- Trees: recursion on trees, depth-first and breadth-first, binary search trees.
- Graphs: adjacency lists, BFS, DFS, components and grids.
- Heaps revisited: top-k, merging streams, running median.
- Greedy: when the obvious choice is right, and how to tell.
- Dynamic programming: remembering answers, from memo to table.
- Backtracking: enumerating arrangements with undo and pruning.
- Practice: 143 more problems, one page per topic, plus three hard pages.
Each page ends with exercises. Some of their checks use inputs large enough that the slow approach runs out of time; the problem statement says so when that is the case.