<commit_before>---
title: Generate Pascals Triangle with Ruby Enumerator
author: <NAME>
published: false
---

Sequences need not be numbers either. Pascal’s Triangle, a favorite of mine, represents a [“triangular array of the binomial coefficients”]. We can model this in Ruby as an array of arrays. The first array member is `[1]`. Each successive array (or “row”) will increase in size, and each array member will be the sum of the member at the same index `n` in the `k-1` row, where `k` is the current row, and the `n-1` member in the `k-1` row or 0. In other words, add the number above and the number above to the left (or zero) to get the current number. We can express the first 5 rows as follows:

''[
''[1],
''[1, 1],
''[1, 2, 1],
''[1, 3, 3, 1],
''[1, 4, 6, 4, 1]
'']

Let’s solve this with Ruby. To be certain, there are a number of approaches to generating Pascal’s Triangle, including both recursive and iterative solutions, so consider the following as just one technique that emphasizes use of the Enumerable API.

Pascal’s Triangle is another infinite sequence where each item is a row. Starting with the first row, `[1]`, we can write a Ruby method that will generate the next row `[1, 1]`. Let’s write this in a way so it will be possible to generate any row `k` from row `k-1`. Here’s what the usage of this method will look like:

''pascal_row([1])
''=> [1, 1]
''pascal_row([1, 3, 3, 1])
''=> [1, 4, 6, 4, 1]

Let’s engineer how we’ll do this by deconstructing down this fifth row: `[1, 4, 6, 4, 1]`. Each member is the sum of `n` and `n-1` (or 0) from the previous row, `[1, 3, 3, 1]`. Therefore, we can rewrite the fifth row as

''[(0 + 1), (1 + 3), (3 + 3), (3 + 1), (1 + 0)]
''=> [1, 4, 6, 4, 1]

We could create that construct from a nested array of number pairs and collecting the sum of each pair like so:

''[[0, 1], [1, 3], [3, 3], [3, 1], [1, 0]].collect { |a, b| a + b }
''=> [1, 4, 6, 4, 1]

Look closely at those array pairs and we see a pattern. Take the first element<commit_msg>Additional text for pascal's triangle post<commit_after>---
title: Generate Pascals Triangle with Ruby Enumerator
author: <NAME>
published: false
---

Sequences need not be numbers either. Pascal’s Triangle, a favorite of mine, represents a [“triangular array of the binomial coefficients”]. We can model this in Ruby as an array of arrays. The first array member is `[1]`. Each successive array (or “row”) will increase in size, and each array member will be the sum of the member at the same index `n` in the `k-1` row, where `k` is the current row, and the `n-1` member in the `k-1` row or 0. In other words, add the number above and the number above to the left (or zero) to get the current number. We can express the first 5 rows as follows:

''[
''[1],
''[1, 1],
''[1, 2, 1],
''[1, 3, 3, 1],
''[1, 4, 6, 4, 1]
'']

Let’s solve this with Ruby. To be certain, there are a number of approaches to generating Pascal’s Triangle, including both recursive and iterative solutions, so consider the following as just one technique that emphasizes use of the Enumerable API.

Pascal’s Triangle is another infinite sequence where each item is a row. Starting with the first row, `[1]`, we can write a Ruby method that will generate the next row `[1, 1]`. Let’s write this in a way so it will be possible to generate any row `k` from row `k-1`. Here’s what the usage of this method will look like:

''pascal_row([1])
''=> [1, 1]
''pascal_row([1, 3, 3, 1])
''=> [1, 4, 6, 4, 1]

Let’s engineer how we’ll do this by deconstructing down this fifth row: `[1, 4, 6, 4, 1]`. Each member is the sum of `n` and `n-1` (or 0) from the previous row, `[1, 3, 3, 1]`. Therefore, we can rewrite the fifth row as

''[(0 + 1), (1 + 3), (3 + 3), (3 + 1), (1 + 0)]
''=> [1, 4, 6, 4, 1]

We could create that construct from a nested array of number pairs and collecting the sum of each pair like so:

''[[0, 1], [1, 3], [3, 3], [3, 1], [1, 0]].collect { |a, b| a + b }
''=> [1, 4, 6, 4, 1]

Look closely at those array pairs for a pattern: the first members of each pair form the array `[0, 1, 3, 3, 1]` and the second members of each pair are `[1, 3, 3, 1, 0]`. These are instances of row 4 augmented by prepending or appending zero. Given these two arrays, we can form the nested array of pairs with the `Enumerable#zip` method:

[0, 1, 3, 3, 1].zip([1, 3, 3, 1, 0])
=> [[0, 1], [1, 3], [3, 3], [3, 1], [1, 0]]

Let's extract a variable to represent row 4:

```ruby
row = [1, 3, 3, 1]
([0] + row).zip(row + [0])
=> [[0, 1], [1, 3], [3, 3], [3, 1], [1, 0]]
```

Putting it altogether, we can now produce the fifth row from the fourth:

```ruby
row = [1, 3, 3, 1]
([0] + row).zip(row + [0]).collect { |a, b| a + b }
=> [1, 4, 6, 4, 1]
```

Let's confirm this expression works with for other row conversions:

```ruby
row = [1]
([0] + row).zip(row + [0]).collect { |a, b| a + b }
=> [1, 1]

row = [1, 1]
([0] + row).zip(row + [0]).collect { |a, b| a + b }
=> [1, 2, 1]

row = [1, 2, 1]
([0] + row).zip(row + [0]).collect { |a, b| a + b }
=> [1, 3, 3, 1]
```

Yes! We now have the implementation for our method to produce any row
for Pascal's Triangle given the preceding row:

```ruby
def pascal_row(row)
  ([0] + row).zip(row + [0]).collect { |a, b| a + b }
end
```


{'fraction_non_alphanumeric': 0.1508464903937607, 'fraction_numerical': 0.045272969374167776, 'mean_word_length': 3.724168912848158, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3836970', 'n_tokens_mistral': 1984, 'n_tokens_neox': 1802, 'n_words': 941}