{ "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 }