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

aimorris's solution

to Pascal's Triangle in the CoffeeScript Track

Published at Jan 30 2020 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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[6]).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
  rows: (num) ->
    if (num == 0) then [] else @getTriangle num

  getTriangle: (num) ->
    last = @getRow(last) for i in [1..num]

  getRow: (last = []) ->
    @getNumber(last[i-1], last[i]) for i in [0..last.length]

  getNumber: (x, y) -> x + y || 1


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?