Skip to content

Commit 10ba0a9

Browse files
authored
Add pascals-triangle (#135)
1 parent 379f058 commit 10ba0a9

11 files changed

Lines changed: 247 additions & 1 deletion

File tree

bin/add-practice-exercise

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ curl -s -o "canonical-data/${slug}.json" "$github"
9191

9292

9393
camel_slug=$(perl -pe 's/(?:^|-)([a-z])/\u$1/g' <<< "$slug")
94-
cp -v exercises/shared/templates/spec_generator.moon exercises/practice/${slug}/.meta/spec_generator.moon
94+
template=$(< exercises/shared/templates/spec_generator.moon)
95+
template=${template//'${camel_slug}'/"${camel_slug}"}
96+
echo "$template" > exercises/practice/${slug}/.meta/spec_generator.moon
9597

9698

9799
cat << NEXT_STEPS

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,14 @@
490490
"prerequisites": [],
491491
"difficulty": 4
492492
},
493+
{
494+
"slug": "pascals-triangle",
495+
"name": "Pascal's Triangle",
496+
"uuid": "f64601d6-c1fb-4582-8963-fe5510fb2d97",
497+
"practices": [],
498+
"prerequisites": [],
499+
"difficulty": 4
500+
},
493501
{
494502
"slug": "prism",
495503
"name": "Prism",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Instructions
2+
3+
Your task is to output the first N rows of Pascal's triangle.
4+
5+
[Pascal's triangle][wikipedia] is a triangular array of positive integers.
6+
7+
In Pascal's triangle, the number of values in a row is equal to its row number (which starts at one).
8+
Therefore, the first row has one value, the second row has two values, and so on.
9+
10+
The first (topmost) row has a single value: `1`.
11+
Subsequent rows' values are computed by adding the numbers directly to the right and left of the current position in the previous row.
12+
13+
If the previous row does _not_ have a value to the left or right of the current position (which only happens for the leftmost and rightmost positions), treat that position's value as zero (effectively "ignoring" it in the summation).
14+
15+
## Example
16+
17+
Let's look at the first 5 rows of Pascal's Triangle:
18+
19+
```text
20+
1
21+
1 1
22+
1 2 1
23+
1 3 3 1
24+
1 4 6 4 1
25+
```
26+
27+
The topmost row has one value, which is `1`.
28+
29+
The leftmost and rightmost values have only one preceding position to consider, which is the position to its right respectively to its left.
30+
With the topmost value being `1`, it follows from this that all the leftmost and rightmost values are also `1`.
31+
32+
The other values all have two positions to consider.
33+
For example, the fifth row's (`1 4 6 4 1`) middle value is `6`, as the values to its left and right in the preceding row are `3` and `3`:
34+
35+
[wikipedia]: https://en.wikipedia.org/wiki/Pascal%27s_triangle
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Introduction
2+
3+
With the weather being great, you're not looking forward to spending an hour in a classroom.
4+
Annoyed, you enter the class room, where you notice a strangely satisfying triangle shape on the blackboard.
5+
Whilst waiting for your math teacher to arrive, you can't help but notice some patterns in the triangle: the outer values are all ones, each subsequent row has one more value than its previous row and the triangle is symmetrical.
6+
Weird!
7+
8+
Not long after you sit down, your teacher enters the room and explains that this triangle is the famous [Pascal's triangle][wikipedia].
9+
10+
Over the next hour, your teacher reveals some amazing things hidden in this triangle:
11+
12+
- It can be used to compute how many ways you can pick K elements from N values.
13+
- It contains the Fibonacci sequence.
14+
- If you color odd and even numbers differently, you get a beautiful pattern called the [Sierpiński triangle][wikipedia-sierpinski-triangle].
15+
16+
The teacher implores you and your classmates to look up other uses, and assures you that there are lots more!
17+
At that moment, the school bell rings.
18+
You realize that for the past hour, you were completely absorbed in learning about Pascal's triangle.
19+
You quickly grab your laptop from your bag and go outside, ready to enjoy both the sunshine _and_ the wonders of Pascal's triangle.
20+
21+
[wikipedia]: https://en.wikipedia.org/wiki/Pascal%27s_triangle
22+
[wikipedia-sierpinski-triangle]: https://en.wikipedia.org/wiki/Sierpi%C5%84ski_triangle
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"pascals_triangle.moon"
8+
],
9+
"test": [
10+
"pascals_triangle_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
14+
]
15+
},
16+
"blurb": "Compute Pascal's triangle up to a given number of rows.",
17+
"source": "Pascal's Triangle at Wolfram Math World",
18+
"source_url": "https://www.wolframalpha.com/input/?i=Pascal%27s+triangle"
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FACTORIALS = {[0]: 1, [1]: 1}
2+
3+
factorial = (n) ->
4+
if not FACTORIALS[n]
5+
FACTORIALS[n] = n * factorial(n - 1)
6+
FACTORIALS[n]
7+
8+
choose = (n, k) ->
9+
factorial(n) // factorial(k) // factorial(n - k)
10+
11+
{
12+
rows: (n) ->
13+
[ [choose(i, j) for j = 0, i] for i = 0, n - 1]
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
int_list = (list) -> "{#{table.concat list, ', '}}"
2+
3+
int_lists = (lists, level) ->
4+
if #lists == 0
5+
'{}'
6+
elseif #lists == 1
7+
"{#{int_list lists[1]}}"
8+
else
9+
rows = [indent int_list(row) .. ',', level + 1 for row in *lists]
10+
table.insert rows, 1, '{'
11+
table.insert rows, indent '}', level
12+
table.concat rows, '\n'
13+
14+
15+
{
16+
module_name: 'PascalsTriangle',
17+
18+
generate_test: (case, level) ->
19+
lines = {
20+
"result = PascalsTriangle.#{case.property} #{case.input.count}",
21+
"expected = #{int_lists case.expected, level}",
22+
"assert.are.same expected, result"
23+
}
24+
table.concat [indent line, level for line in *lines], '\n'
25+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[9920ce55-9629-46d5-85d6-4201f4a4234d]
13+
description = "zero rows"
14+
15+
[70d643ce-a46d-4e93-af58-12d88dd01f21]
16+
description = "single row"
17+
18+
[a6e5a2a2-fc9a-4b47-9f4f-ed9ad9fbe4bd]
19+
description = "two rows"
20+
21+
[97206a99-79ba-4b04-b1c5-3c0fa1e16925]
22+
description = "three rows"
23+
24+
[565a0431-c797-417c-a2c8-2935e01ce306]
25+
description = "four rows"
26+
27+
[06f9ea50-9f51-4eb2-b9a9-c00975686c27]
28+
description = "five rows"
29+
30+
[c3912965-ddb4-46a9-848e-3363e6b00b13]
31+
description = "six rows"
32+
33+
[6cb26c66-7b57-4161-962c-81ec8c99f16b]
34+
description = "ten rows"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
rows: (n) ->
3+
error 'Implement me'
4+
}

0 commit comments

Comments
 (0)