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

rebelwarrior's solution

to Pascal's Triangle in the CoffeeScript Track

Published at Jun 13 2019 · 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.

pascal-triangle-spec.coffee

Pascal = require './example.coffee'

describe 'Pascal', ->
  it 'with one row', ->
    arr = new Pascal(1)
    expect(arr.array).toEqual [[1]]

  xit 'with two rows', ->
    arr = new Pascal(2)
    expect(arr.array).toEqual [[1], [1,1]]

  xit 'with three rows', ->
    arr = new Pascal(3)
    expect(arr.array).toEqual [[1], [1,1], [1,2,1]]

  xit 'with four rows', ->
    arr = new Pascal(4)
    expect(arr.array).toEqual [[1], [1,1], [1,2,1], [1,3,3,1]]

  xit 'with five rows', ->
    arr = new Pascal(5)
    expect(arr.array).toEqual [[1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]

  xit 'should equal 20th row', ->
    arr = new Pascal(20)
    expect(arr.array[19]).toEqual [1, 19, 171, 969, 3876, 11628, 27132, 50388, 75582, 92378, 92378, 75582, 50388, 27132, 11628, 3876, 969, 171, 19, 1]
class Pascal 
    constructor: (rows)->
        return [] if rows <= 0
        previousRow = [] 
        result = new Array(rows)
        result.fill([])
        newResult = result.map((r)=> 
            previousRow = @nextRow(previousRow)
            previousRow
        )
        @array = newResult

    array:
        @array

    nextRow: (previousRow)-> 
        result = new Array(previousRow.length + 1)
        result.fill(0)
        result[0] = 1
        result[previousRow.length] = 1 
        rewResult = result.map((n, index)=> 
            prevIndex = index - 1
            if n == 0
                previousRow[prevIndex] + previousRow[index]
            else 
                n
        )
        return rewResult
        

module.exports = Pascal

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?