diff --git a/config.json b/config.json index ddde15d..7db3f4a 100644 --- a/config.json +++ b/config.json @@ -493,6 +493,14 @@ "integers" ] }, + { + "slug": "pascals-triangle", + "name": "Pascals Triangle", + "uuid": "2de80883-1847-4fbe-aa33-0b773f356273", + "practices": [], + "prerequisites": [], + "difficulty": 4 + }, { "slug": "dnd-character", "name": "D&D Character", diff --git a/exercises/practice/pascals-triangle/.docs/instructions.md b/exercises/practice/pascals-triangle/.docs/instructions.md new file mode 100644 index 0000000..f556785 --- /dev/null +++ b/exercises/practice/pascals-triangle/.docs/instructions.md @@ -0,0 +1,14 @@ +# Instructions + +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 +``` diff --git a/exercises/practice/pascals-triangle/.meta/config.json b/exercises/practice/pascals-triangle/.meta/config.json new file mode 100644 index 0000000..e3fb085 --- /dev/null +++ b/exercises/practice/pascals-triangle/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "source/pascals_triangle.d" + ], + "test": [ + "source/pascals_triangle.d" + ], + "example": [ + "example/pascals_triangle.d" + ] + }, + "blurb": "Compute Pascal's triangle up to a given number of rows.", + "source": "Pascal's Triangle at Wolfram Math World", + "source_url": "https://www.wolframalpha.com/input/?i=Pascal%27s+triangle" +} diff --git a/exercises/practice/pascals-triangle/.meta/tests.toml b/exercises/practice/pascals-triangle/.meta/tests.toml new file mode 100644 index 0000000..2db0ee5 --- /dev/null +++ b/exercises/practice/pascals-triangle/.meta/tests.toml @@ -0,0 +1,34 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[9920ce55-9629-46d5-85d6-4201f4a4234d] +description = "zero rows" + +[70d643ce-a46d-4e93-af58-12d88dd01f21] +description = "single row" + +[a6e5a2a2-fc9a-4b47-9f4f-ed9ad9fbe4bd] +description = "two rows" + +[97206a99-79ba-4b04-b1c5-3c0fa1e16925] +description = "three rows" + +[565a0431-c797-417c-a2c8-2935e01ce306] +description = "four rows" + +[06f9ea50-9f51-4eb2-b9a9-c00975686c27] +description = "five rows" + +[c3912965-ddb4-46a9-848e-3363e6b00b13] +description = "six rows" + +[6cb26c66-7b57-4161-962c-81ec8c99f16b] +description = "ten rows" diff --git a/exercises/practice/pascals-triangle/dub.sdl b/exercises/practice/pascals-triangle/dub.sdl new file mode 100644 index 0000000..834d1ad --- /dev/null +++ b/exercises/practice/pascals-triangle/dub.sdl @@ -0,0 +1,2 @@ +name "pascals-triangle" +buildRequirements "disallowDeprecations" diff --git a/exercises/practice/pascals-triangle/example/pascals_triangle.d b/exercises/practice/pascals-triangle/example/pascals_triangle.d new file mode 100644 index 0000000..0aaf478 --- /dev/null +++ b/exercises/practice/pascals-triangle/example/pascals_triangle.d @@ -0,0 +1,20 @@ +module pascals_triangle; + +pure int[][] rows(int count) +{ + int[][] result = new int[][](count); + int[] previous; + for (int i = 0; i < count; ++i) + { + int[] current = new int[](i + 1); + current[0] = 1; + for (int j = 1; j < i; ++j) + { + current[j] = previous[j - 1] + previous[j]; + } + current[i] = 1; + result[i] = current; + previous = current; + } + return result; +} diff --git a/exercises/practice/pascals-triangle/source/pascals_triangle.d b/exercises/practice/pascals-triangle/source/pascals_triangle.d new file mode 100644 index 0000000..a2b8441 --- /dev/null +++ b/exercises/practice/pascals-triangle/source/pascals_triangle.d @@ -0,0 +1,101 @@ +module pascals_triangle; + +pure int[][] rows(int count) +{ + // implement this function +} + +unittest +{ + immutable int allTestsEnabled = 0; + + // Zero rows + { + int[][] expected = [ + ]; + assert(rows(0) == expected); + } + + static if (allTestsEnabled) + { + // Single row + { + int[][] expected = [ + [1], + ]; + assert(rows(1) == expected); + } + + // Two rows + { + int[][] expected = [ + [1], + [1, 1], + ]; + assert(rows(2) == expected); + } + + // Three rows + { + int[][] expected = [ + [1], + [1, 1], + [1, 2, 1], + ]; + assert(rows(3) == expected); + } + + // Four rows + { + int[][] expected = [ + [1], + [1, 1], + [1, 2, 1], + [1, 3, 3, 1], + ]; + assert(rows(4) == expected); + } + + // Five rows + { + int[][] expected = [ + [1], + [1, 1], + [1, 2, 1], + [1, 3, 3, 1], + [1, 4, 6, 4, 1], + ]; + assert(rows(5) == expected); + } + + // Six rows + { + int[][] expected = [ + [1], + [1, 1], + [1, 2, 1], + [1, 3, 3, 1], + [1, 4, 6, 4, 1], + [1, 5, 10, 10, 5, 1], + ]; + assert(rows(6) == expected); + } + + // Ten rows + { + int[][] expected = [ + [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], + ]; + assert(rows(10) == expected); + } + } +}