Created
March 5, 2022 14:26
-
-
Save ibrahimsha23/c72c9a75142ed477a3c6db674af78fcc to your computer and use it in GitHub Desktop.
Calculate subsets of elements in 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
import copy | |
import string | |
alphabet_string = list(string.ascii_lowercase) | |
def subsets(arr): | |
""" | |
Calculate subsets of elements in an array .. | |
arr = ['a', 'b'] | |
output = ['', 'a', 'b', 'ab',] | |
""" | |
expected = 2**len(arr) | |
print(f'result of N integer is 2**N ==> {expected}') | |
data_box = [arr[0]] | |
prev_box = [] | |
for i in arr[1:]: | |
prev_box = copy.deepcopy(data_box) | |
for j in data_box: | |
prev_box.append(i + "," +j) | |
prev_box.append(i) | |
data_box, prev_box = prev_box, [] | |
print(data_box) | |
print(f'{len(data_box)}==== {expected}') | |
subsets(alphabet_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment