Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pascals-triangle exercise #395

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,17 @@
"math"
]
},
{
"slug": "pascals-triangle",
"name": "Pascal's Triangle",
"uuid": "cc71a6d5-efb2-4054-bf72-102b3e96e0bc",
"practices": [],
"prerequisites": [],
"difficulty": 4,
"topics": [
"math"
]
},
{
"slug": "affine-cipher",
"name": "Affine Cipher",
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": [
"keiravillekode"
],
"files": {
"solution": [
"pascals-triangle.el"
],
"test": [
"pascals-triangle-test.el"
],
"example": [
".meta/example.el"
]
},
"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"
}
23 changes: 23 additions & 0 deletions exercises/practice/pascals-triangle/.meta/example.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
;;; pascals-triangle.el --- Pascal's Triangle (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(require 'cl-lib)

(defun rows (count)
(let ((result (make-vector count nil))
(current nil)
(previous nil))
(cl-loop for r below count
do (setq current (make-vector (1+ r) 1))
do (cl-loop for c from 1 below r
do (aset current c (+ (aref previous (1- c)) (aref previous c))))
do (aset result r current)
do (setq previous current))
result))

(provide 'pascals-triangle)
;;; pascals-triangle.el ends here

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"
69 changes: 69 additions & 0 deletions exercises/practice/pascals-triangle/pascals-triangle-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
;;; pascals-triangle-test.el --- Tests for Pascal's Triangle (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(load-file "pascals-triangle.el")
(declare-function rows "pascals-triangle.el" (count))


(ert-deftest zero-rows ()
(should (equal [] (rows 0))))


(ert-deftest single-row ()
(should (equal [[1]] (rows 1))))


(ert-deftest two-rows ()
(should (equal [[1]
[1 1]] (rows 2))))


(ert-deftest three-rows ()
(should (equal [[1]
[1 1]
[1 2 1]] (rows 3))))


(ert-deftest four-rows ()
(should (equal [[1]
[1 1]
[1 2 1]
[1 3 3 1]] (rows 4))))


(ert-deftest five-rows ()
(should (equal [[1]
[1 1]
[1 2 1]
[1 3 3 1]
[1 4 6 4 1]] (rows 5))))


(ert-deftest six-rows ()
(should (equal [[1]
[1 1]
[1 2 1]
[1 3 3 1]
[1 4 6 4 1]
[1 5 10 10 5 1]] (rows 6))))


(ert-deftest ten-rows ()
(should (equal [[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]] (rows 10))))


(provide 'pascals-triangle-test)
;;; pascals-triangle-test.el ends here
14 changes: 14 additions & 0 deletions exercises/practice/pascals-triangle/pascals-triangle.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
;;; pascals-triangle.el --- Pascal's Triangle (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(defun rows (count)
(error "Delete this S-Expression and write your own implementation"))


(provide 'pascals-triangle)
;;; pascals-triangle.el ends here