Skip to content

Instantly share code, notes, and snippets.

@ibrahimsha23
Created March 5, 2022 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibrahimsha23/c72c9a75142ed477a3c6db674af78fcc to your computer and use it in GitHub Desktop.
Save ibrahimsha23/c72c9a75142ed477a3c6db674af78fcc to your computer and use it in GitHub Desktop.
Calculate subsets of elements in an array
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