-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Create GenerateSubsets #1581
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
base: master
Are you sure you want to change the base?
Create GenerateSubsets #1581
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
function generateSubsets(nums) { | ||
const subsets = []; | ||
|
||
function backtrack(start = 0, currentSubset = []) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I would call this backtracking; to me it's just ordinary recursion:
|
||
// Add the currentSubset to the subsets array | ||
subsets.push([...currentSubset]); | ||
|
||
// Explore all possible subsets by backtracking | ||
for (let i = start; i < nums.length; i++) { | ||
currentSubset.push(nums[i]); | ||
backtrack(i + 1, currentSubset); | ||
currentSubset.pop(); | ||
} | ||
} | ||
|
||
backtrack(); | ||
return subsets; | ||
} | ||
|
||
const inputSet = [1, 2, 3]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example should be removed from this file (or converted into a JSDoc example). |
||
const allSubsets = generateSubsets(inputSet); | ||
|
||
console.log("All Subsets:"); | ||
for (const subset of allSubsets) { | ||
console.log(subset); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// we define a generateSubsets function that uses backtracking to generate all possible subsets of a given set (array of numbers). The backtrack function is a recursive function that explores all possible subsets by considering or excluding each element. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's in the other file. Remove this comment from the test file. |
||
|
||
import { generateSubsets } from '../GenerateSubsets' | ||
|
||
describe('generateSubsets', () => { | ||
it('should generate all subsets for an array', () => { | ||
const inputSet = [1, 2, 3]; | ||
const expectedSubsets = [ | ||
[], | ||
[1], | ||
[1, 2], | ||
[1, 2, 3], | ||
[1, 3], | ||
[2], | ||
[2, 3], | ||
[3] | ||
]; | ||
|
||
const subsets = generateSubsets(inputSet); | ||
|
||
// Check if the generated subsets are deep equal to the expected subsets | ||
expect(subsets).to.deep.have.members(expectedSubsets); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arr
, notnums
. The implementation doesn't need to make any assumptions about the values inarr
.