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

Clarify duplicate policy for Ch4 triples exercise #313

Open
milesfrain opened this issue Mar 6, 2021 · 1 comment
Open

Clarify duplicate policy for Ch4 triples exercise #313

milesfrain opened this issue Mar 6, 2021 · 1 comment

Comments

@milesfrain
Copy link
Member

This exercise currently rejects duplicate triples, but does not mention that requirement. Some options to fix:

  1. 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 ]"
  2. Clarify that duplicate triples should be generated, and modify tests and solutions. I think this adds unnecessary noise.
  3. 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

@Zelenya
Copy link

Zelenya commented Nov 11, 2023

Note: this is chapter 5 now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants