> For the complete documentation index, see [llms.txt](https://aaronchen.gitbook.io/daq/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://aaronchen.gitbook.io/daq/chapter-2-splitting-bars.md).

# Chapter 2: Splitting Bars

### Recursive problem #2: Split <a href="#recursive-problem-2-split" id="recursive-problem-2-split"></a>

Imagine a bar of solid gold.

![](https://aaron_chen.gitbooks.io/divide-and-conquer/content/assets/gold1.png)

You want to find out how much it weighs, but you're held back by one thing:

**You only know that a square of gold weighs 5 pounds.**

Your gold bar is larger than a square.

As we did before, let's go ahead and define a function. The function **split** has one parameter: **gold\_bar**, the bar of gold.

**split** aims to return how many pounds the gold bar weighs.

```python
def split(gold_bar):
    #this function should return how much "gold_bar" weighs!
```

If our problem is weighing the gold bar, let's think about how we can make the problem smaller! True to the function's name, why don't we try **splitting** the gold bar...

In this case, we can **split** the gold bar into:

![](https://aaron_chen.gitbooks.io/divide-and-conquer/content/assets/gold2.png)

```python
   square_of_gold        +        (rest_of_bar)
```

The square of gold is a known quantity: we know it weighs 5 pounds. The rest of the bar is an unsolved problem. However, it's smaller than our original problem! It's important to realize that when added together, these two **smaller** problems are **equivalent** to our original problem, but they're easier for us to solve.

We can take the rest of the bar and **split** it again, and repeat this process. What does this look like in code?

```python
def split(gold_bar):

    return 5 + split(rest_of_bar)
```

We keep splitting the rest of the bar in order to make our problem **smaller**.

```python
split(gold_bar) -> split(rest_of_bar) -> split(rest of rest_of_bar) -> ...
```

Again, because we are making the problem smaller and smaller; this line is the **recursive call**.

For now, let's continue splitting.

![](https://aaron_chen.gitbooks.io/divide-and-conquer/content/assets/gold2.png)

![](https://aaron_chen.gitbooks.io/divide-and-conquer/content/assets/gold3.png)

```python
square_of_gold    +        split(rest_of_bar)

square_of_gold    +        (square_of_gold        +     split(rest_of_bar))
```

And split the rest of the bar again....

![](https://aaron_chen.gitbooks.io/divide-and-conquer/content/assets/gold4.png)

We are left with

```python
square_of_gold   +  ( square_of_gold   + (square_of_gold   +   split(rest_of_bar))
```

but the rest of the bar is a square! That's a known quantity! This is **equivalent** to

![](https://aaron_chen.gitbooks.io/divide-and-conquer/content/assets/gold5.png)

```python
square_of_gold   +   ( square_of_gold   +    (square_of_gold       +    square_of_gold))
```

Which is 5 + 5 + 5 + 5 = 20 pounds!

By now you should have realized that our **base case** is when we have split the gold bar enough times so that it is a square, a known quantity. In code:

```python
def split(gold_bar):
    if gold_bar == square_of_gold:
        return 5

    return 5 + split(rest_of_bar)
```

Now, every time we split, we see if the **gold\_bar** we're splitting is a square; if it is, we **return** it, and we're finished! Otherwise, we return square of gold + **split**(rest of bar), knowing that **split** will make our problem smaller.

If you're still confused or not completely sold, here's a quick recap to show you what's happening:

```python
split(gold_bar) -> square + split(rest_of_bar)                        #4 squares = 20 pounds
                                V
                        (square + split(rest_of_previous_bar))        #3 squares
                                    V
                             (square + split(rest_of_previous_bar))   #2 squares
                                         V
                                       square
```

### Great, now what? <a href="#great-now-what" id="great-now-what"></a>

So, what was the point of this example? The goal of this example was to introduce the idea of **split**ting a problem into two smaller ones; In our example, we **split** the problem of weighing a gold bar into the problems of:

1. Weighing a square of gold
2. Weighing the rest of the bar

The first problem we could solve easily, and the second could be **split** further. In order to generalize this kind of thinking, consider this **split**ting strategy as splitting a problem into:

1. A solved or easily solvable problem
2. A problem that can be further **split**

I'll give concrete examples in the next chapter, to show you how we can use this approach in real code.
