import numpy as np
##
## Partitions: numbers and sets.
## Arrangements.
## Combinations.
## Permutations.
## Subsets.
##
# iteratively variant
def partitionNum():
def init():
stack[level] = 0
def succ():
global sum
if stack[level] < n - sum:
stack[level] += 1
return True
else:
sum -= stack[level-1]
def valid():
global sum
if stack[level] <= n - sum:
sum += stack[level]
return True
return False
def sol():
return sum == n
def printf():
global sum
for i in range(1,level+1):
print(stack[i], end = " ")
print()
sum -= stack[level]
def solve():
global level
level = 1
init()
while level > 0:
s = True
v = False
while s and not v:
s = succ()
if s:
v = valid()
if s:
if sol():
printf()
else:
level +=1
init()
else:
level -= 1
global sum
sum = 0
n = 3
stack = [0] * (n+1)
solve()
partitionNum()
print()
def partitionSet():
def init(level):
stack[level] = 0
def succ(level):
if stack[level] < stack[level-1] + 1:
stack[level] += 1
return True
return False
def valid(level):
return True
def sol(level):
return level == n
def printf(level):
maxv = np.max(stack)
for i in range(1, maxv+1):
print("{", end="")
for k in range(1, n+1):
if i == stack[k]:
print(k, end = ",")
print("\b}", end="")
print()
def solve(level):
init(level)
while succ(level):
if valid(level):
if sol(level):
printf(level)
else:
solve(level+1)
n = 3
stack = [0] * (n+1)
solve(1)
partitionSet()
print()
def arrange():
def ok(level):
for i in range(1, level):
if stack[level] == stack[i]:
return False
return True
def solve(level):
if level == k + 1:
for i in range(1, k + 1):
print(stack[i], end = " ")
print()
else:
for i in range(1, n + 1):
stack[level] = i
if ok(level):
solve(level+1)
n = 3
k = 2
stack = [0] * (n+1)
solve(1)
arrange()
def comb():
def ok(level):
for i in range(1, level):
if stack[level] == stack[i]:
return False
return True
def solve(level):
if level == k + 1:
for i in range(1, k + 1):
print(stack[i], end = " ")
print()
else:
for i in range(stack[level-1]+1, n + 1):
stack[level] = i
if ok(level):
solve(level+1)
n = 3
k = 2
stack = [0] * (n+1)
solve(1)
comb()
def perm():
def ok(level):
for i in range(1, level):
if stack[level] == stack[i]:
return False
return True
def solve(level):
if level == n + 1:
for i in range(1, n + 1):
print(stack[i], end = " ")
print()
else:
for i in range(1, n + 1):
stack[level] = i
if ok(level):
solve(level+1)
n = 3
stack = [0] * (n+1)
solve(1)
perm()
def _subsets():
def solve(level):
if level <= n:
for i in range(stack[level-1]+1, n+1):
stack[level] = i
for i in range(1,level+1):
print(stack[i], end=" ")
print()
solve(level+1)
n = 3
stack = [0] * (n+1)
solve(1)
_subsets()
def subsets():
def solve(working_set, level, n):
if level == n:
s = {k for k in working_set if working_set[k] == 1}
solutions.append(s)
else:
level += 1
for i in [0,1]:
working_set[level] = i
solve(working_set, level, n)
n = 3
solutions = []
solve({}, 0, n)
print(solutions)
subsets()
aW1wb3J0IG51bXB5IGFzIG5wCiMjCiMjIFBhcnRpdGlvbnM6IG51bWJlcnMgYW5kIHNldHMuCiMjIEFycmFuZ2VtZW50cy4KIyMgQ29tYmluYXRpb25zLgojIyBQZXJtdXRhdGlvbnMuCiMjIFN1YnNldHMuCiMjCgojIGl0ZXJhdGl2ZWx5IHZhcmlhbnQKZGVmIHBhcnRpdGlvbk51bSgpOgogICAgZGVmIGluaXQoKToKICAgICAgICBzdGFja1tsZXZlbF0gPSAwCiAgICBkZWYgc3VjYygpOgogICAgICAgIGdsb2JhbCBzdW0KICAgICAgICBpZiBzdGFja1tsZXZlbF0gPCBuIC0gc3VtOgogICAgICAgICAgICBzdGFja1tsZXZlbF0gKz0gMQogICAgICAgICAgICByZXR1cm4gVHJ1ZQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHN1bSAtPSBzdGFja1tsZXZlbC0xXQoKICAgIGRlZiB2YWxpZCgpOgogICAgICAgIGdsb2JhbCBzdW0KICAgICAgICBpZiBzdGFja1tsZXZlbF0gPD0gbiAtIHN1bToKICAgICAgICAgICBzdW0gKz0gc3RhY2tbbGV2ZWxdCiAgICAgICAgICAgcmV0dXJuIFRydWUKICAgICAgICByZXR1cm4gRmFsc2UKCiAgICBkZWYgc29sKCk6CiAgICAgICAgcmV0dXJuIHN1bSA9PSBuCgogICAgZGVmIHByaW50ZigpOgogICAgICAgIGdsb2JhbCBzdW0KICAgICAgICBmb3IgaSBpbiByYW5nZSgxLGxldmVsKzEpOgogICAgICAgICAgICBwcmludChzdGFja1tpXSwgZW5kID0gIiAiKQogICAgICAgIHByaW50KCkKICAgICAgICBzdW0gLT0gc3RhY2tbbGV2ZWxdCgogICAgZGVmIHNvbHZlKCk6CiAgICAgICAgZ2xvYmFsIGxldmVsCiAgICAgICAgbGV2ZWwgPSAxCiAgICAgICAgaW5pdCgpCiAgICAgICAgd2hpbGUgbGV2ZWwgPiAwOgogICAgICAgICAgICBzID0gVHJ1ZQogICAgICAgICAgICB2ID0gRmFsc2UKICAgICAgICAgICAgd2hpbGUgcyBhbmQgbm90IHY6CiAgICAgICAgICAgICAgICBzID0gc3VjYygpCiAgICAgICAgICAgICAgICBpZiBzOgogICAgICAgICAgICAgICAgICAgIHYgPSB2YWxpZCgpCiAgICAgICAgICAgIGlmIHM6CiAgICAgICAgICAgICAgICBpZiBzb2woKToKICAgICAgICAgICAgICAgICAgICBwcmludGYoKQogICAgICAgICAgICAgICAgZWxzZToKICAgICAgICAgICAgICAgICAgICBsZXZlbCArPTEKICAgICAgICAgICAgICAgICAgICBpbml0KCkKICAgICAgICAgICAgZWxzZToKICAgICAgICAgICAgICAgIGxldmVsIC09IDEKICAgIGdsb2JhbCBzdW0KICAgIHN1bSA9IDAKICAgIG4gPSAzCiAgICBzdGFjayA9IFswXSAqIChuKzEpCiAgICBzb2x2ZSgpCgpwYXJ0aXRpb25OdW0oKQoKcHJpbnQoKQoKZGVmIHBhcnRpdGlvblNldCgpOgoKICAgIGRlZiBpbml0KGxldmVsKToKICAgICAgICBzdGFja1tsZXZlbF0gPSAwCgogICAgZGVmIHN1Y2MobGV2ZWwpOgogICAgICAgIGlmIHN0YWNrW2xldmVsXSA8IHN0YWNrW2xldmVsLTFdICsgMToKICAgICAgICAgICAgc3RhY2tbbGV2ZWxdICs9IDEKICAgICAgICAgICAgcmV0dXJuIFRydWUKICAgICAgICByZXR1cm4gRmFsc2UKCiAgICBkZWYgdmFsaWQobGV2ZWwpOgogICAgICAgIHJldHVybiBUcnVlCgogICAgZGVmIHNvbChsZXZlbCk6CiAgICAgICAgcmV0dXJuIGxldmVsID09IG4KCiAgICBkZWYgcHJpbnRmKGxldmVsKToKICAgICAgICBtYXh2ID0gbnAubWF4KHN0YWNrKQogICAgICAgIGZvciBpIGluIHJhbmdlKDEsIG1heHYrMSk6CiAgICAgICAgICAgIHByaW50KCJ7IiwgZW5kPSIiKQogICAgICAgICAgICBmb3IgayBpbiByYW5nZSgxLCBuKzEpOgogICAgICAgICAgICAgICAgaWYgaSA9PSBzdGFja1trXToKICAgICAgICAgICAgICAgICAgICBwcmludChrLCBlbmQgPSAiLCIpCiAgICAgICAgICAgIHByaW50KCJcYn0iLCBlbmQ9IiIpCiAgICAgICAgcHJpbnQoKQoKICAgIGRlZiBzb2x2ZShsZXZlbCk6CiAgICAgICAgaW5pdChsZXZlbCkKICAgICAgICB3aGlsZSBzdWNjKGxldmVsKToKICAgICAgICAgICAgaWYgdmFsaWQobGV2ZWwpOgogICAgICAgICAgICAgICAgaWYgc29sKGxldmVsKToKICAgICAgICAgICAgICAgICAgICBwcmludGYobGV2ZWwpCiAgICAgICAgICAgICAgICBlbHNlOgogICAgICAgICAgICAgICAgICAgIHNvbHZlKGxldmVsKzEpCiAgICBuID0gMwogICAgc3RhY2sgPSBbMF0gKiAobisxKQogICAgc29sdmUoMSkKCnBhcnRpdGlvblNldCgpCgpwcmludCgpCgpkZWYgYXJyYW5nZSgpOgogICAgZGVmIG9rKGxldmVsKToKICAgICAgICBmb3IgaSBpbiByYW5nZSgxLCBsZXZlbCk6CiAgICAgICAgICAgIGlmIHN0YWNrW2xldmVsXSA9PSBzdGFja1tpXToKICAgICAgICAgICAgICAgIHJldHVybiBGYWxzZQogICAgICAgIHJldHVybiBUcnVlCgogICAgZGVmIHNvbHZlKGxldmVsKToKICAgICAgICBpZiBsZXZlbCA9PSBrICsgMToKICAgICAgICAgICAgZm9yIGkgaW4gcmFuZ2UoMSwgayArIDEpOgogICAgICAgICAgICAgICAgcHJpbnQoc3RhY2tbaV0sIGVuZCA9ICIgIikKICAgICAgICAgICAgcHJpbnQoKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIGZvciBpIGluIHJhbmdlKDEsIG4gKyAxKToKICAgICAgICAgICAgICAgIHN0YWNrW2xldmVsXSA9IGkKICAgICAgICAgICAgICAgIGlmIG9rKGxldmVsKToKICAgICAgICAgICAgICAgICAgICBzb2x2ZShsZXZlbCsxKQogICAgbiA9IDMKICAgIGsgPSAyCiAgICBzdGFjayA9IFswXSAqIChuKzEpCiAgICBzb2x2ZSgxKQphcnJhbmdlKCkKCmRlZiBjb21iKCk6CiAgICBkZWYgb2sobGV2ZWwpOgogICAgICAgIGZvciBpIGluIHJhbmdlKDEsIGxldmVsKToKICAgICAgICAgICAgaWYgc3RhY2tbbGV2ZWxdID09IHN0YWNrW2ldOgogICAgICAgICAgICAgICAgcmV0dXJuIEZhbHNlCiAgICAgICAgcmV0dXJuIFRydWUKCiAgICBkZWYgc29sdmUobGV2ZWwpOgogICAgICAgIGlmIGxldmVsID09IGsgKyAxOgogICAgICAgICAgICBmb3IgaSBpbiByYW5nZSgxLCBrICsgMSk6CiAgICAgICAgICAgICAgICBwcmludChzdGFja1tpXSwgZW5kID0gIiAiKQogICAgICAgICAgICBwcmludCgpCiAgICAgICAgZWxzZToKICAgICAgICAgICAgZm9yIGkgaW4gcmFuZ2Uoc3RhY2tbbGV2ZWwtMV0rMSwgbiArIDEpOgogICAgICAgICAgICAgICAgc3RhY2tbbGV2ZWxdID0gaQogICAgICAgICAgICAgICAgaWYgb2sobGV2ZWwpOgogICAgICAgICAgICAgICAgICAgIHNvbHZlKGxldmVsKzEpCiAgICBuID0gMwogICAgayA9IDIKICAgIHN0YWNrID0gWzBdICogKG4rMSkKICAgIHNvbHZlKDEpCmNvbWIoKQoKCmRlZiBwZXJtKCk6CiAgICBkZWYgb2sobGV2ZWwpOgogICAgICAgIGZvciBpIGluIHJhbmdlKDEsIGxldmVsKToKICAgICAgICAgICAgaWYgc3RhY2tbbGV2ZWxdID09IHN0YWNrW2ldOgogICAgICAgICAgICAgICAgcmV0dXJuIEZhbHNlCiAgICAgICAgcmV0dXJuIFRydWUKCiAgICBkZWYgc29sdmUobGV2ZWwpOgogICAgICAgIGlmIGxldmVsID09IG4gKyAxOgogICAgICAgICAgICBmb3IgaSBpbiByYW5nZSgxLCBuICsgMSk6CiAgICAgICAgICAgICAgICBwcmludChzdGFja1tpXSwgZW5kID0gIiAiKQogICAgICAgICAgICBwcmludCgpCiAgICAgICAgZWxzZToKICAgICAgICAgICAgZm9yIGkgaW4gcmFuZ2UoMSwgbiArIDEpOgogICAgICAgICAgICAgICAgc3RhY2tbbGV2ZWxdID0gaQogICAgICAgICAgICAgICAgaWYgb2sobGV2ZWwpOgogICAgICAgICAgICAgICAgICAgIHNvbHZlKGxldmVsKzEpCiAgICBuID0gMwogICAgc3RhY2sgPSBbMF0gKiAobisxKQogICAgc29sdmUoMSkKcGVybSgpCgpkZWYgX3N1YnNldHMoKToKICAgIGRlZiBzb2x2ZShsZXZlbCk6CiAgICAgICAgaWYgbGV2ZWwgPD0gbjoKICAgICAgICAgICAgZm9yIGkgaW4gcmFuZ2Uoc3RhY2tbbGV2ZWwtMV0rMSwgbisxKToKICAgICAgICAgICAgICAgIHN0YWNrW2xldmVsXSA9IGkKICAgICAgICAgICAgICAgIGZvciBpIGluIHJhbmdlKDEsbGV2ZWwrMSk6CiAgICAgICAgICAgICAgICAgICAgcHJpbnQoc3RhY2tbaV0sIGVuZD0iICIpCiAgICAgICAgICAgICAgICBwcmludCgpCiAgICAgICAgICAgICAgICBzb2x2ZShsZXZlbCsxKQoKICAgIG4gPSAzCiAgICBzdGFjayA9IFswXSAqIChuKzEpCiAgICBzb2x2ZSgxKQpfc3Vic2V0cygpCgpkZWYgc3Vic2V0cygpOgogICAgZGVmIHNvbHZlKHdvcmtpbmdfc2V0LCBsZXZlbCwgbik6CiAgICAgICAgaWYgbGV2ZWwgPT0gbjoKICAgICAgICAgICAgcyA9IHtrIGZvciBrIGluIHdvcmtpbmdfc2V0IGlmIHdvcmtpbmdfc2V0W2tdID09IDF9CiAgICAgICAgICAgIHNvbHV0aW9ucy5hcHBlbmQocykKICAgICAgICBlbHNlOgogICAgICAgICAgICBsZXZlbCArPSAxCiAgICAgICAgICAgIGZvciBpIGluIFswLDFdOgogICAgICAgICAgICAgICAgd29ya2luZ19zZXRbbGV2ZWxdID0gaQogICAgICAgICAgICAgICAgc29sdmUod29ya2luZ19zZXQsIGxldmVsLCBuKQogICAgbiA9IDMKICAgIHNvbHV0aW9ucyA9IFtdCiAgICBzb2x2ZSh7fSwgMCwgbikKICAgIHByaW50KHNvbHV0aW9ucykKc3Vic2V0cygpCg==
1 1 1
1 2
2 1
3
{1,2,3,}
{1,2,}{3,}
{1,3,}{2,}
{1,}{2,3,}
{1,}{2,}{3,}
1 2
1 3
2 1
2 3
3 1
3 2
1 2
1 3
2 3
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
1
1 2
1 2 3
1 3
2
2 3
3
[set(), {3}, {2}, {2, 3}, {1}, {1, 3}, {1, 2}, {1, 2, 3}]