Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of bkuri

bkuri's solution

to Pascal's Triangle in the CoffeeScript Track

Published at Aug 28 2020 · 0 comments
Instructions
Test suite
Solution

Compute Pascal's triangle up to a given number of rows.

In Pascal's Triangle each number is computed by adding the numbers to the right and left of the current position in the previous row.

    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1
# ... etc

Refer to the Exercism CoffeScript page for getting started with CoffeeScript.

In order to run the test, you can run the test file from the exercise directory:

jasmine-node --coffee .

Source

Pascal's Triangle at Wolfram Math World http://mathworld.wolfram.com/PascalsTriangle.html

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

pascals-triangle.spec.coffee

Pascal = require './pascals-triangle'

describe 'Pascal', ->
  it 'one row', ->
    pascal = new PascalsTriangle()
    expect(pascal.rows(0)).toEqual []

  xit 'two rows', ->
    pascal = new PascalsTriangle()
    expect(pascal.rows(1)).toEqual [[1]]

  xit 'three rows', ->
    pascal = new PascalsTriangle()
    expect(pascal.rows(2)).toEqual [[1], [1,1]]

  xit 'four rows', ->
    pascal = new PascalsTriangle()
    expect(pascal.rows(3)).toEqual [[1], [1,1], [1,2,1]]

  xit 'five rows', ->
    pascal = new PascalsTriangle()
    expect(pascal.rows(4)).toEqual [[1], [1,1], [1,2,1], [1,3,3,1]]

  xit 'five rows', ->
    pascal = new PascalsTriangle()
    expect(pascal.rows(5)).toEqual [[1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]

  xit 'six rows', ->
    pascal = new PascalsTriangle()
    expect(pascal.rows(6)).toEqual [[1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1], [1, 5, 10, 10, 5, 1]]

  xit 'ten rows', ->
    pascal = new PascalsTriangle()
    expect(pascal.rows(10)).toEqual [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1], [1, 6, 15, 20, 15, 6, 1], [1, 7, 21, 35, 35, 21, 7, 1], [1, 8, 28, 56, 70, 56, 28, 8, 1], [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]]
class PascalsTriangle
  constructor: ->
    return

  rows: (len) ->
    return [] if (len is 0)
    return [[ 1 ]] if (len is 1)
    result = []

    renderRow = (row) ->
      renderCol = (col) ->
        return 1 if (col is 0) or (col is row-1)
        r = result[ row-2 ]
        return r[ col-1 ] + r[ col ]

      result.push [ 0...row ].map(renderCol)
      return

    [ 1..len ].map renderRow
    return result

module.exports = PascalsTriangle

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?