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

Refactor: Combination #5225

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

samuelfac
Copy link
Contributor

Related to #5164

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter
Copy link

codecov-commenter commented Jun 12, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 2 lines in your changes missing coverage. Please review.

Project coverage is 39.20%. Comparing base (cdb3aff) to head (df5d877).

Files Patch % Lines
...va/com/thealgorithms/backtracking/Combination.java 83.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #5225      +/-   ##
============================================
- Coverage     39.22%   39.20%   -0.02%     
+ Complexity     2401     2400       -1     
============================================
  Files           517      517              
  Lines         15522    15517       -5     
  Branches       2958     2957       -1     
============================================
- Hits           6089     6084       -5     
  Misses         9140     9140              
  Partials        293      293              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@samuelfac samuelfac marked this pull request as ready for review June 12, 2024 18:10
Comment on lines +21 to +23
assert result != null;
assertEquals(1, (int) result.get(0).getFirst());
assertEquals(2, (int) result.get(1).getFirst());
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert result != null;
assertEquals(1, (int) result.get(0).getFirst());
assertEquals(2, (int) result.get(1).getFirst());
assertEquals(Arrays.asList(new TreeSet<>(Arrays.asList(1)), new TreeSet<>(Arrays.asList(2))), result);

Comment on lines +29 to +32
assert result != null;
Integer[] arr = result.getFirst().toArray(new Integer[2]);
assertEquals(1, (int) arr[0]);
assertEquals(2, (int) arr[1]);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert result != null;
Integer[] arr = result.getFirst().toArray(new Integer[2]);
assertEquals(1, (int) arr[0]);
assertEquals(2, (int) arr[1]);
assertEquals(Arrays.asList(new TreeSet<>(Arrays.asList(1, 2))), result);

Comment on lines +27 to 29
} else if (combinationSize == 0) {
return null;
}
Copy link
Member

Choose a reason for hiding this comment

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

I do not see a reason to return null here. You can just remove this branch, so the result would be Arrays.asList(new TreeSet<Integer>()).

The problem is ArrayCombination. The method combination will need an update.

Copy link
Member

Choose a reason for hiding this comment

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

Could you please:

  • add a test case with data being an empty array and combinationSize being positive,
  • add a test case with data being an empty array and combinationSize being zero,
  • add a test case with data being [1, 2, 3] and combinationSize being 2,
  • add a test case with data being an array of strings.

import java.util.List;
import java.util.TreeSet;

/**
* Finds all permutations of given array
* @author Alan Piao (<a href="https://github.com/cpiao3">git-Alan Piao</a>)
* Finds all combinations of a given array of unique elements.
Copy link
Member

Choose a reason for hiding this comment

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

This is not verified anywhere. Maybe the input type should be Set<T>? Or maybe this statement is not needed. I think what we are aiming for it to mimic the itertools.combinations from python.

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

3 participants