-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
281 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,19 @@ | ||
pub fn powerset(set: Vec<i32>) -> Vec<Vec<i32>> { | ||
let mut result: Vec<Vec<i32>> = vec![]; | ||
let len: usize = set.len(); | ||
let powerset_len: usize = 2usize.pow(len as u32); | ||
|
||
for binary_combination in 0..powerset_len { | ||
let mut subset: Vec<i32> = vec![]; | ||
|
||
for position in 0..len { // Check if the j-th bit is set in the binary combination | ||
if binary_combination & (1 << position) != 0 { | ||
subset.push(set[position]); | ||
} | ||
} | ||
|
||
result.push(subset); | ||
} | ||
|
||
return result; | ||
} |
This file contains 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