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

(16) New exercise | Permutations #444

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

nikitarevenco
Copy link
Contributor

@nikitarevenco nikitarevenco commented Mar 23, 2024

Because

It was decided to add new recursion exercises

This PR

  • Adds exercise 16

Previous

Issue

Related to #27265

Additional Information

Pull Request Requirements

  • I have thoroughly read and understand The Odin Project Contributing Guide
  • The title of this PR follows the location of change: brief description of change format, e.g. 01_helloWorld: Update test cases
  • The Because section summarizes the reason for this PR
  • The This PR section has a bullet point list describing the changes in this PR
  • If this PR addresses an open issue, it is linked in the Issue section
  • If this PR includes any changes that affect the solution of an exercise, I've also updated the solution in the /solutions folder

@nikitarevenco nikitarevenco marked this pull request as draft March 23, 2024 15:01
@nikitarevenco nikitarevenco changed the title New exercise | sumSquares (16) New exercise | sumSquares Mar 23, 2024
@nikitarevenco nikitarevenco changed the title (16) New exercise | sumSquares (16) New exercise | Permutations Mar 24, 2024
@nikitarevenco nikitarevenco marked this pull request as ready for review April 4, 2024 17:10
@CouchofTomato CouchofTomato requested review from a team and bycdiaz and removed request for a team April 5, 2024 14:26
@nikitarevenco nikitarevenco mentioned this pull request Apr 6, 2024
6 tasks
Copy link
Contributor

@MaoShizhong MaoShizhong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haven't looked at the the solution yet but one (technically 2) important change needed for the test suite below.

[actual, expected] = outcome(
[1, 2, 3, 4],
[
[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've got an extra nested array here in the "expected array" that's not needed, but also your outcome function results in the test passing despite this being an incorrect expected outcome.

That being said, even after fixing this, you don't need an outcome helper function with an afterEach that uses .toBe. You can just use the .toEqual matcher that's designed exactly for this purpose.

test('Works for array of size two', () => {
  expect(permutations([1, 2])).toEqual([
    [1, 2],
    [2, 1],
  ]);
});

Having all the tests as per the above format will all pass (and will also fail if you keep the unintended extra nested array in the last test "expected" array.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've got an extra nested array here in the "expected array" that's not needed, but also your outcome function results in the test passing despite this being an incorrect expected outcome.

That being said, even after fixing this, you don't need an outcome helper function with an afterEach that uses .toBe. You can just use the .toEqual matcher that's designed exactly for this purpose.

test('Works for array of size two', () => {
  expect(permutations([1, 2])).toEqual([
    [1, 2],
    [2, 1],
  ]);
});

Having all the tests as per the above format will all pass (and will also fail if you keep the unintended extra nested array in the last test "expected" array.

Yes but the problem with that is, what happens if the learner's solution returns the correct array but in a different order?

You've got an extra nested array here in the "expected array" that's not needed, but also your outcome function results in the test passing despite this being an incorrect expected outcome.

That being said, even after fixing this, you don't need an outcome helper function with an afterEach that uses .toBe. You can just use the .toEqual matcher that's designed exactly for this purpose.

test('Works for array of size two', () => {
  expect(permutations([1, 2])).toEqual([
    [1, 2],
    [2, 1],
  ]);
});

Having all the tests as per the above format will all pass (and will also fail if you keep the unintended extra nested array in the last test "expected" array.

OHhhhhhhh wow, I had no idea the following test would pass:

    expect([1, 2]).toEqual([2, 1])

I was thinking that the order that the learner's function returns the array in should not matter, and kind of just assumed that toEqual checks for the order too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point I missed about the toEqual order. That can be remedied by sorting both sides

test('Works for array of size two', () => {
  expect(permutations([1, 2]).sort()).toEqual(
    [
      [1, 2],
      [2, 1],
    ].sort()
  );
});

Note the .sort() for the expect is after the permutations call, not on the array passed into the permutations call.
Do that, and the order of the matched array won't matter.

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

Successfully merging this pull request may close these issues.

None yet

2 participants