Skip to content

Instantly share code, notes, and snippets.

@Niceblack
Created October 28, 2014 14:49
Show Gist options
  • Save Niceblack/1c8e500e0fba33c542d2 to your computer and use it in GitHub Desktop.
Save Niceblack/1c8e500e0fba33c542d2 to your computer and use it in GitHub Desktop.
# coding: utf-8
# python 2.x
# Печать всех подмножеств множетсва {1, 2, ..., N}
def print_subsets(n, k = 1, s = ""):
print "{" + s + "}" # С пустым множеством
for i in range(k, n + 1):
#print "{" + s + ("," if s else "") + str(i) + "}" # Без пустого множества
print_subsets(n, i + 1, s + ("," if s else "") + str(i))
# Использование:
print_subsets(4)
# Вывод:
# {}
# {1}
# {1,2}
# {1,2,3}
# {1,2,3,4}
# {1,2,4}
# {1,3}
# {1,3,4}
# {1,4}
# {2}
# {2,3}
# {2,3,4}
# {2,4}
# {3}
# {3,4}
# {4}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment