forked from exercism/racket
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Pascal's Triangle (exercism#360)
* Pascal's triangle for Racket
- Loading branch information
1 parent
f80870e
commit 3a728d3
Showing
7 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"authors": ["blakelewis"], | ||
"files": { | ||
"solution": [ | ||
"pascals-triangle.rkt" | ||
], | ||
"test": [ | ||
"pascals-triangle-test.rkt" | ||
], | ||
"example": [ | ||
".meta/example.rkt" | ||
] | ||
}, | ||
"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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#lang racket | ||
|
||
(provide rows) | ||
|
||
(define (next-row bottom) | ||
(do ([xs bottom (cdr xs)] | ||
[left 0 (car xs)] | ||
[row '() (cons (+ left (car xs)) row)]) | ||
((null? xs) (cons 1 row)))) | ||
|
||
(define (rows height) | ||
(for/fold ([triangle '(())] | ||
#:result (if (null? triangle) | ||
triangle | ||
(cdr (reverse triangle)))) | ||
([r (in-range height)]) | ||
(cons (next-row (car triangle)) triangle))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
68 changes: 68 additions & 0 deletions
68
exercises/practice/pascals-triangle/pascals-triangle-test.rkt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#lang racket/base | ||
|
||
(require "pascals-triangle.rkt") | ||
|
||
(module+ test | ||
(require rackunit rackunit/text-ui)) | ||
|
||
(module+ test | ||
(define suite | ||
(test-suite "pascal's triangle tests" | ||
|
||
(test-equal? "zero rows" | ||
(rows 0) | ||
'()) | ||
|
||
(test-equal? "single row" | ||
(rows 1) | ||
'((1))) | ||
|
||
(test-equal? "two rows" | ||
(rows 2) | ||
'((1) | ||
(1 1))) | ||
|
||
(test-equal? "three rows" | ||
(rows 3) | ||
'((1) | ||
(1 1) | ||
(1 2 1))) | ||
|
||
(test-equal? "four rows" | ||
(rows 4) | ||
'((1) | ||
(1 1) | ||
(1 2 1) | ||
(1 3 3 1))) | ||
|
||
(test-equal? "five rows" | ||
(rows 5) | ||
'((1) | ||
(1 1) | ||
(1 2 1) | ||
(1 3 3 1) | ||
(1 4 6 4 1))) | ||
|
||
(test-equal? "six rows" | ||
(rows 6) | ||
'((1) | ||
(1 1) | ||
(1 2 1) | ||
(1 3 3 1) | ||
(1 4 6 4 1) | ||
(1 5 10 10 5 1))) | ||
|
||
(test-equal? "ten rows" | ||
(rows 10) | ||
'((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))))) | ||
|
||
(run-tests suite)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#lang racket | ||
|
||
(provide rows) | ||
|
||
(define (rows height) | ||
(error "Not implemented yet")) |