Skip to content

Clarify duplicate policy for Ch4 triples exercise #313

Description

@milesfrain

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

Activity

  1. Zelenya commented on Nov 11, 2023

    @Zelenya

    Note: this is chapter 5 now

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions