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
Pascal's Triangle at Wolfram Math World http://mathworld.wolfram.com/PascalsTriangle.html
This exercise has been tested on Julia versions >=1.0.
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
In [ ]:
# submit
function triangle(n)
end
In [ ]:
using Test
# include("pascals-triangle.jl")
rows = [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]]
@testset "$i row(s)" for i in 1:4
@test triangle(i) == rows[1:i]
end
@testset "special cases" begin
@test_throws DomainError triangle(-1)
@test isempty(triangle(0))
end
To submit your exercise, you need to save your solution in a file called pascals-triangle.jl
before using the CLI.
You can either create it manually or use the following functions, which will automatically write every notebook cell that starts with # submit
to the file pascals-triangle.jl
.
In [ ]:
# using Pkg; Pkg.add("Exercism")
# using Exercism
# Exercism.create_submission("pascals-triangle")