Skip to content

Created a Permutation Function in the Recursion package. Closed the issue #7322#7412

Open
trynafind-sumanyu wants to merge 8 commits into
TheAlgorithms:masterfrom
trynafind-sumanyu:master
Open

Created a Permutation Function in the Recursion package. Closed the issue #7322#7412
trynafind-sumanyu wants to merge 8 commits into
TheAlgorithms:masterfrom
trynafind-sumanyu:master

Conversation

@trynafind-sumanyu
Copy link
Copy Markdown

@trynafind-sumanyu trynafind-sumanyu commented May 4, 2026

Issue #7322

  • 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 algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 4, 2026

Codecov Report

❌ Patch coverage is 92.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.55%. Comparing base (2616e09) to head (a8a8e13).

Files with missing lines Patch % Lines
...java/com/thealgorithms/recursion/Permutations.java 92.00% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7412      +/-   ##
============================================
+ Coverage     79.54%   79.55%   +0.01%     
- Complexity     7178     7186       +8     
============================================
  Files           798      799       +1     
  Lines         23467    23492      +25     
  Branches       4617     4621       +4     
============================================
+ Hits          18666    18690      +24     
- Misses         4055     4056       +1     
  Partials        746      746              

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@DenizAltunkapan
Copy link
Copy Markdown
Member

@trynafind-sumanyu there exists another permutation class: https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/Permutation.java
Whats the difference between these?

@trynafind-sumanyu
Copy link
Copy Markdown
Author

Hey @DenizAltunkapan,

I wasn’t aware that a permutation implementation already existed under the backtracking package, as the issue specifically referenced the recursion package, where no such file was present. Based on that, I proceeded with adding a new implementation there.

Additionally, I introduced handling for duplicate elements to ensure that only unique permutations are generated. This behavior was not present in the existing backtracking/Permutation.java implementation.

Please let me know if you’d prefer aligning this with the existing implementation or consolidating the changes.

@DenizAltunkapan
Copy link
Copy Markdown
Member

DenizAltunkapan commented May 26, 2026

Hey @DenizAltunkapan,

I wasn’t aware that a permutation implementation already existed under the backtracking package, as the issue specifically referenced the recursion package, where no such file was present. Based on that, I proceeded with adding a new implementation there.

Additionally, I introduced handling for duplicate elements to ensure that only unique permutations are generated. This behavior was not present in the existing backtracking/Permutation.java implementation.

Please let me know if you’d prefer aligning this with the existing implementation or consolidating the changes.

@trynafind-sumanyu You can align these in the existing implementation. Just make sure that there aren't any duplicates. Thank you and sorry for the late reply : )

Copy link
Copy Markdown
Contributor

@prashantpiyush1111 prashantpiyush1111 left a comment

Choose a reason for hiding this comment

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

Overall the implementation is well-structured with good
test coverage. However, the main concern is the duplicate
class — please align with the existing
backtracking/Permutation.java as suggested by
@DenizAltunkapan. Please address all inline comments. 🙌

* @throws NullPointerException if items is null
* @throws IllegalArgumentException if any element in items is null
*/
public static <T> List<List<T>> permutations(T[] items) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As mentioned by @DenizAltunkapan, a Permutation class already
exists at backtracking/Permutation.java. Please align your
unique permutations logic into the existing implementation
instead of creating a separate class.

return;
}

Set<T> seen = new HashSet<>();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

HashSet relies on hashCode() and equals() for deduplication.
For custom objects that don't override these methods,
duplicates may not be detected correctly.
Consider documenting this assumption in JavaDoc or
using explicit comparison instead.

import java.util.List;
import org.junit.jupiter.api.Test;

class PermutationsTest {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Test file name should be PermutationsTest.java not
Permutations.java — as per TheAlgorithms naming convention.

*/
public final class Permutations {

private Permutations() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Private constructor is not tested — this is causing
2 uncovered lines in Codecov.

Add:
@test
void testConstructorThrowsException() {
assertThrows(UnsupportedOperationException.class, () -> {
var constructor = Permutations.class.getDeclaredConstructor();
constructor.setAccessible(true);
constructor.newInstance();
});
}

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.

4 participants