Initial upload

This commit is contained in:
2022-08-24 14:28:45 +02:00
parent c67653ddee
commit 57bc7b0289
370 changed files with 18479 additions and 0 deletions

View File

@@ -0,0 +1 @@
{"track":"julia","exercise":"pascals-triangle","id":"802dc59e8eb8489f9fa601b08be5a1ac","url":"https://exercism.io/my/solutions/802dc59e8eb8489f9fa601b08be5a1ac","handle":"halfdan","is_requester":true,"auto_approve":false}

View File

@@ -0,0 +1,25 @@
# Pascal's Triangle
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.
```text
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
# ... etc
```
## Source
Pascal's Triangle at Wolfram Math World [http://mathworld.wolfram.com/PascalsTriangle.html](http://mathworld.wolfram.com/PascalsTriangle.html)
## Version compatibility
This exercise has been tested on Julia versions >=1.0.
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

View File

@@ -0,0 +1,68 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": ["# Pascal's Triangle\n", "\n", "Compute Pascal's triangle up to a given number of rows.\n", "\n", "In Pascal's Triangle each number is computed by adding the numbers to\n", "the right and left of the current position in the previous row.\n", "\n", "```text\n", " 1\n", " 1 1\n", " 1 2 1\n", " 1 3 3 1\n", "1 4 6 4 1\n", "# ... etc\n", "```\n", "\n", "## Source\n", "\n", "Pascal's Triangle at Wolfram Math World [http://mathworld.wolfram.com/PascalsTriangle.html](http://mathworld.wolfram.com/PascalsTriangle.html)\n", "\n", "## Version compatibility\n", "This exercise has been tested on Julia versions >=1.0.\n", "\n", "## Submitting Incomplete Solutions\n", "It's possible to submit an incomplete solution so you can see how others have completed the exercise."]
},
{
"cell_type": "markdown",
"metadata": {},
"source": ["## Your solution"]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": ["# submit\n", "function triangle(n)\n", "\n", "end"]
},
{
"cell_type": "markdown",
"metadata": {},
"source": ["## Test suite"]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": ["using Test\n", "\n", "# include(\"pascals-triangle.jl\")\n", "\n", "const rows = [[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]]\n", "\n", "@testset \"$i row(s)\" for i in eachindex(rows)\n", " @test triangle(i) == @view rows[1:i]\n", "end\n", "\n", "@testset \"special cases\" begin\n", " @test_throws DomainError triangle(-1)\n", " @test isempty(triangle(0))\n", "end"]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prepare submission\n",
"To submit your exercise, you need to save your solution in a file called `pascals-triangle.jl` before using the CLI.\n",
"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`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# using Pkg; Pkg.add(\"Exercism\")\n",
"# using Exercism\n",
"# Exercism.create_submission(\"pascals-triangle\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.3.0",
"language": "julia",
"name": "julia-1.3"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.3.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@@ -0,0 +1,9 @@
function triangle(n)
n == 0 && return []
n < 0 && throw(DomainError("Invalid n given"))
rows = [[1]]
for _ 2:n
push!(rows, [a+b for (a,b) zip([0;rows[end]], [rows[end]; 0])])
end
rows
end

View File

@@ -0,0 +1,14 @@
using Test
include("pascals-triangle.jl")
const rows = [[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]]
@testset "$i row(s)" for i in eachindex(rows)
@test triangle(i) == @view rows[1:i]
end
@testset "special cases" begin
@test_throws DomainError triangle(-1)
@test isempty(triangle(0))
end