Last active
January 1, 2025 18:07
-
-
Save Beyarz/9970ddd53774d8e4739d682f679cd362 to your computer and use it in GitHub Desktop.
Calculate power set from an array
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
arr = [1, 2, 3] | |
# => [1, 2, 3] | |
def power_set(arr) | |
result = [[]] | |
arr.each do |element| | |
result += result.map do |num| | |
num + [element] | |
end | |
end | |
result | |
end | |
# => :power_set | |
power_set(arr) | |
# => [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]] |
Author
Beyarz
commented
Jan 1, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment