Skip to content

Commit

Permalink
Add pascals-triangle exercise (#269)
Browse files Browse the repository at this point in the history
  • Loading branch information
glennj authored Jun 15, 2024
1 parent 839a69c commit 362e53a
Show file tree
Hide file tree
Showing 8 changed files with 826 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "pascals-triangle",
"name": "Pascal's Triangle",
"uuid": "20e94baf-b776-4ac1-a195-3857c8c0e747",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "word-count",
"name": "Word Count",
Expand Down
14 changes: 14 additions & 0 deletions exercises/practice/pascals-triangle/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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
```
19 changes: 19 additions & 0 deletions exercises/practice/pascals-triangle/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"glennj"
],
"files": {
"solution": [
"pascals-triangle.awk"
],
"test": [
"test-pascals-triangle.bats"
],
"example": [
".meta/example.awk"
]
},
"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"
}
12 changes: 12 additions & 0 deletions exercises/practice/pascals-triangle/.meta/example.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$1 > 0 {
triangle[1][1] = 1
print "1"

for (i = 2; i <= $1; i++) {
for (j = 1; j <= i; j++) {
triangle[i][j] = 0 + triangle[i-1][j-1] + triangle[i-1][j]
printf "%s%d", (j == 1 ? "" : " "), triangle[i][j]
}
print ""
}
}
34 changes: 34 additions & 0 deletions exercises/practice/pascals-triangle/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
Loading

0 comments on commit 362e53a

Please sign in to comment.