-
Notifications
You must be signed in to change notification settings - Fork 20k
Adding class for generating all subsequences from a given List #5194
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
82b0ba1
Adding class for generating all subsequences from a given List
822a694
Fix test data format
c27bd88
Fix braces wrong placement
5140ca2
Merge branch 'master' into subsequences_generator
alxkm 959a6d1
Fix "Utility classes should not have a public or default constructor."
f6c2cd0
Merge remote-tracking branch 'origin/subsequences_generator' into sub…
82084d8
Fix checkstyle " Class Subsequence should be declared as final."
839c589
Renaming class Subsequence to SubsequenceFinder. Refactored test to P…
2b6f77e
Fix formatting
a55f656
Fix formatting
3d95e8f
Merge branch 'master' into subsequences_generator
alxkm 6ffdc6b
Fix formatting
927b362
Fix import ordering
3c0e12e
Renaming method generate all.
d4b69df
Fix formatting
3d0d76b
style: add assertion to avoid potential infinite loop
vil02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/com/thealgorithms/backtracking/SubsequenceFinder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.thealgorithms.backtracking; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Class generates all subsequences for a given list of elements using backtracking | ||
*/ | ||
public final class SubsequenceFinder { | ||
private SubsequenceFinder() { | ||
} | ||
|
||
/** | ||
* Find all subsequences of given list using backtracking | ||
* | ||
* @param sequence a list of items on the basis of which we need to generate all subsequences | ||
* @param <T> the type of elements in the array | ||
* @return a list of all subsequences | ||
*/ | ||
public static <T> List<List<T>> generateAll(List<T> sequence) { | ||
List<List<T>> allSubSequences = new ArrayList<>(); | ||
if (sequence.isEmpty()) { | ||
allSubSequences.add(new ArrayList<>()); | ||
return allSubSequences; | ||
} | ||
List<T> currentSubsequence = new ArrayList<>(); | ||
backtrack(sequence, currentSubsequence, 0, allSubSequences); | ||
return allSubSequences; | ||
} | ||
|
||
/** | ||
* Iterate through each branch of states | ||
* We know that each state has exactly two branching | ||
* It terminates when it reaches the end of the given sequence | ||
* | ||
* @param sequence all elements | ||
* @param currentSubsequence current subsequence | ||
* @param index current index | ||
* @param allSubSequences contains all sequences | ||
* @param <T> the type of elements which we generate | ||
*/ | ||
private static <T> void backtrack(List<T> sequence, List<T> currentSubsequence, final int index, List<List<T>> allSubSequences) { | ||
if (index == sequence.size()) { | ||
allSubSequences.add(new ArrayList<>(currentSubsequence)); | ||
return; | ||
} | ||
|
||
backtrack(sequence, currentSubsequence, index + 1, allSubSequences); | ||
currentSubsequence.add(sequence.get(index)); | ||
backtrack(sequence, currentSubsequence, index + 1, allSubSequences); | ||
currentSubsequence.removeLast(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/test/java/com/thealgorithms/backtracking/SubsequenceFinderTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.thealgorithms.backtracking; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertIterableEquals; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
public class SubsequenceFinderTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource("getTestCases") | ||
void testGenerateAll(TestCase testData) { | ||
final var actual = SubsequenceFinder.generateAll(testData.input()); | ||
assertIterableEquals(testData.expected(), actual); | ||
} | ||
|
||
static Stream<TestCase> getTestCases() { | ||
return Stream.of(new TestCase(new ArrayList<>(), List.of(List.of())), new TestCase(List.of(1, 2), List.of(List.of(), List.of(2), List.of(1), List.of(1, 2))), | ||
new TestCase(List.of("A", "B", "C"), List.of(List.of(), List.of("C"), List.of("B"), List.of("B", "C"), List.of("A"), List.of("A", "C"), List.of("A", "B"), List.of("A", "B", "C"))), | ||
new TestCase(List.of(1, 2, 3), List.of(List.of(), List.of(3), List.of(2), List.of(2, 3), List.of(1), List.of(1, 3), List.of(1, 2), List.of(1, 2, 3))), new TestCase(List.of(2, 2), List.of(List.of(), List.of(2), List.of(2), List.of(2, 2)))); | ||
} | ||
|
||
record TestCase(List<Object> input, List<List<Object>> expected) { | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.