Beginner 3
Hello! This is page 5 of the course called “Y Combinator for Non-programmers”. If you just got here, click here to read from the beginning.

# .css-1yxxo5r{color:#43a047;}Beginner 4:The Rules of The Lunchbox

Slide 1 / 18

### Success?

I’ve figured it out!

Looks like the villagers have figured out the patterns for the “Lunchbox” puzzle .

I think I’ve figured out how lunchboxes work!

Really? Then explain to me.

Looking for some other page?

Beginner 3·
·Beginner 5
Slide 2 / 18

### If the bottom two items are the same

Let me explain. First, you compare the bottom two items.

If the bottom two items are the same, then when you run it, we’re left with the top row at the end.

The bottom two items are the same
(Both sushi )
We’re left with the sandwich ,
which was on the top

The bottom two items are the same
(Both pizza )
We’re left with spaghetti and bread ,
which were on the top

So this is the solution for the first Yes / No quiz:

The bottom two items are the same
(Both carrot )
We’re left with the broccoli ,
which was on the top
Slide 3 / 18

### If the bottom two items are different

Now, let’s talk about the case where the bottom two items are different.

In this case, when you run it, we’re left with the bottom-right item at the end.

The bottom two items are different
( and )
We’re left with the chicken ,
which was in the bottom-right

The bottom two items are different ( and )
We’re left with the hot dog ,
which was in the bottom-right

So this is the solution for the second Yes / No quiz:

The bottom two items are different ( and )
We’re left with the eggplant ,
which was in the bottom-right

This has to be the solution. Am I correct?

Slide 4 / 18

### Correct!

Exactly right!

You’re exactly right. Good job!

Yay!

However, don’t celebrate just yet.

Huh? Why not?

Because: Before going to the next page, we still need to formalize the rules of the “Lunchbox” puzzle .

Formalize? What do you mean?

Slide 5 / 18

### Formalize the rules

Here are the patterns you discovered:

• Compare the bottom two items.
• If they’re the same, the top item remains.
• Otherwise, the bottom-right item remains.

They’re not wrong, but they are not detailed enough to solve more difficult puzzles.

Hmm… okay…

In order to solve more complex puzzles, we need to formalize the rules. We must be more detailed.

We have to formalize the rules.
We must be more detailed.

So: I’ll explain to you the formal, more detailed rules of the “Lunchbox” puzzle .

Once you understand it, I’ll give you more complex puzzles to solve.

Okay… show me what the formal rules look like.

Slide 6 / 18

### The formal rules of the“Lunchbox” puzzle

Here are the formal rules of the “Lunchbox” puzzle . There are four steps:

1. Label: t l r

Label: t l r
tlr

2. Match: l r

Match: l r
tlr

3. Copy: t r

Copy: t r
tlr

4. Remove: t l

Remove: t l
tl

Now, let’s talk about each step.

Slide 7 / 18

### Step 1. Label:tlr

We’ll use this lunchbox to explain each step:

First: We label:

• The top items as t (for “Top”)
• The left items as l (for “Left”)
• The right items as r (for “Right”)
tlr
Label items as t l r

Let’s see it in action. Press Next :

The sandwich is labeled as t, and sushi on the bottom are labeled as l and r.

Slide 8 / 18

### Step 2. Match:lr

Second: We check to see if some of l’s and r’s match. If they match, add the sign.

lr
If some of l’s and r’s match, add

Press Next :

tlr

The sushi labeled as l and r now have a sign.

Slide 9 / 18

### Step 3. Copy:tr

Third: We copy t’s to where the matched r’s are.

tr
Copy t’s to matched r’s

Press Next :

tlr

The sandwich (labeled as t) is copied to where the matched r is.

Slide 10 / 18

### Step 4. Remove: tl

Finally: We remove t’s and l’s.

tl
Remove t’s and l’s

Press Next :

tlr

t’s and l’s have turned into .

After removing these, we’re left with just the sandwich .Press Next :

tl

That’s it!

Slide 11 / 18

### What if there are more than one item on the top?

Question: What happens if there are more than one item on the top, like the example below?

There are two items on the top

Answer: In this case, both spaghetti and bread will be labeled as t (for “Top”).

Both labeled as t
ttlr

Then: The pizzas match

There’s a match among l’s and r’s
ttlr

Therefore: Both spaghetti and bread get copied to where the matched r is.

Copy t’s to where the matched r’s are
ttlrr

Finally: t’s and l’s disappear…

Remove t’s and l’s
ttl

And we’re left with spaghetti and bread .

Slide 12 / 18

### Putting it together

To wrap up, let’s go through the entire process from the beginning to the end.

We’ll be using the Run button with the “Show every step” functionality, which automatically shows every step that happens between the beginning and the end.

If you use this button…

You’ll be able to see all steps
automatically

Here’s the previous example with one item on the top. Press Run . (Or press to step through manually.)

Here’s the previous example with two items on the top. Press Run :

Slide 13 / 18

### Summary

Let’s review what we’ve learned so far:

1. Label: t l r

Label: t l r
tlr

2. Match: l r

Match: l r
tlr

3. Copy: t r

Copy: t r
tlr

4. Remove: t l

Remove: t l
tl

I see, now I understand everything clearly.

Good. And by formalizing the rules in detail like this, we’ll be able to solve more complex puzzles.

Slide 14 / 18

### What if there’s no match?

lr
What if there’s no match
between l’s and r’s?

But wait. What happens if l’s and r’s don’t match?

For example, the lunchbox below has no match among l’s andr’s.

Press Next :

tlr
Slide 15 / 18

### Don’t copy, just removet andl

Answer: If l’s and r’s don’t match, skip the “copy” step, and just remove t’s and l’s.

Press Next :

tlr

In this case, we’re left with the chicken . Press Next :

tl
Slide 16 / 18

### Let’s go through all the steps

Review: Let’s go through all the steps again—this time for cases where l’s and r’s don’t match.

Let’s see all the steps
from the beginning to the end

Here’s the previous example with one item on the top. Press Run . (Or press to step through manually.)

Here’s the previous example with two items on the top. Press Run :

Slide 17 / 18

### Summary: If they don’t match

If none of l’s and r’s match: Skip the copy step and simply remove t’s and l’s.

If none of l’s and r’s match…
tlr
Simply remove t’s and l’s
tl
Slide 18 / 18

### That’s it!

That’s it for the formal rules of the “Lunchbox” puzzle .

Ok…

I understood them, but I haven’t fully memorized them yet.

It’s ok. Don’t worry if you haven’t memorized the rules. We’ll do a review when we use them later.

Oh, that’s good to hear.

(Maybe the devil isn’t so evil after all… )

On the next page, I’ll give you more challenging puzzles.

If you want your mathboxes back, you better solve all of them.

Alright, bring it on!

Go to Next PageContinue to Beginner 5
1 more beginner level left!
Beginner 3