cconnected.
Understand the pattern. Find your own way.Find your starting point
Your learning feed/Idea 7 of 218
Ideas this builds on
Start small07 / 218

Have we seen this before?

Picture

Keep a small checklist of places you’ve visited. Before adding the park, ask whether it’s already there. A set keeps one copy of each distinct item.

Predict

Add the park twice to a set. How many copies of the park are there?

Let's look

One. Adding an existing item doesn’t create another copy.

A little Python, if you want it
seen = set()
seen.add("park")
seen.add("park")
assert "park" in seen
assert len(seen) == 1

in asks whether an item is present. A set is useful for membership, not for remembering the order of a route. Python set membership is expected constant time for ordinary well-behaved keys.

Change one rule

We also need to remember how many times we visited the park. Is membership alone enough?

Let's try it

No. We need to attach a count to the place. A dictionary can do that.

Connect

Next, attach a value to a name. Later, visited sets help searches avoid unwanted repeats.