Chapter 9. 8 recursion and dictionaries: Beyond Iteration and Indices

Image

It’s time to take your computational thinking up a notch. And this is the chapter to do it: we’ve been happily coding along with an iterative style of programming—we’ve created data structures like lists and strings and ranges of numbers, and we’ve written code to compute by iterating over them. In this chapter we’re going to look at the world differently, first in terms of computation, and then in terms of data structures. Computationally we’ll look at a style of computing that involves writing code that recurs, or calls itself. We’ll expand the kinds of data structures we can work with by looking at a dictionary-like data type that is more like an associative map than a list. We’ll then put them together and cause all kinds of trouble. Be forewarned: these topics take a while to settle into your brain, but the effort is going to pay off in spades.

A different way to compute

It’s time for some mind-bending activity—you’ve been thinking about the same, iterative style of programming for too long. So let’s expose your brain to a totally different way of thinking about solving problems.

Image

Before we get there, though, let’s take a simple problem and think it through the way we have throughout this book. For ...

Get Head First Learn to Code now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.