This exercise currently rejects duplicate triples, but does not mention that requirement. Some options to fix:
- Clarify that duplicate triples should be avoided. e.g. "Note: Avoid returning duplicate triples, for example if generating
[ 4, 3, 5 ], don't also generate [ 5, 3, 4 ]"
- Clarify that duplicate triples should be generated, and modify tests and solutions. I think this adds unnecessary noise.
- Automatically remove duplicates in the test suite. This might test suite errors a little confusing, since the "actual" result will be different than what the user's function actually returns, although we already have that same potential for confusion with
sort applied in the suite. Returning Set (Set Int) would simplify exercise requirements, but might not be appropriate for this chapter.
Existing exercise text:
(Medium) Write a function triples :: Int -> Array (Array Int) which takes a number n and returns all Pythagorean triples whose components (the a, b and c values) are each less than or equal to n. A Pythagorean triple is an array of numbers [a, b, c] such that a² + b² = c². Hint: Use the guard function in an array comprehension.
https://book.purescript.org/chapter4.html#exercises-2
|
triples :: Int -> Array (Array Int) |
|
triples n = do |
|
i <- 1 .. n |
|
j <- i .. n |
|
k <- j .. n |
|
guard $ i * i + j * j == k * k |
|
pure [ i, j, k ] |
|
suite "Exercise - triples" do |
|
-- Sorting to allow for any ordering |
|
test "single element array result" do |
|
Assert.equal (sort [ [ 3, 4, 5 ] ]) |
|
$ sort |
|
$ triples 5 |
|
test "multiple element array result" do |
|
Assert.equal (sort [ [ 3, 4, 5 ], [ 5, 12, 13 ], [ 6, 8, 10 ] ]) |
|
$ sort |
|
$ triples 13 |
This exercise currently rejects duplicate triples, but does not mention that requirement. Some options to fix:
[ 4, 3, 5 ], don't also generate[ 5, 3, 4 ]"sortapplied in the suite. ReturningSet (Set Int)would simplify exercise requirements, but might not be appropriate for this chapter.Existing exercise text:
https://book.purescript.org/chapter4.html#exercises-2
purescript-book/exercises/chapter4/test/no-peeking/Solutions.purs
Lines 52 to 58 in 6527372
purescript-book/exercises/chapter4/test/Main.purs
Lines 102 to 111 in 6527372