Skip to content

Instantly share code, notes, and snippets.

@LittleWat
Created February 11, 2022 10:35
Show Gist options
  • Save LittleWat/d5515c76002c8e6d06e07df8921937f6 to your computer and use it in GitHub Desktop.
Save LittleWat/d5515c76002c8e6d06e07df8921937f6 to your computer and use it in GitHub Desktop.
How to find subsets without itertools in Python
def find_subsets(arr) :
result = []
n = len(arr)
for i in range(2**n):
binstr = format(i, f"0{n}b")
print(binstr)
tmp = []
for j, onebin in enumerate(binstr):
if onebin == "1":
tmp.append(arr[j])
result.append(tmp)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment