Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Question: Create a Python generator function to generate the power set of a given set.
Answer:

def power_set(input_set):

  n = len(input_set)

  for i in range(1 << n):

    subset = [input_set[j]

      for j in range(n):

        if (i & (1 << j)) > 0]

          yield subset

Example:

power_set({1, 2, 3})  # Output: [], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]
Is it helpful? Yes No

Most helpful rated by users:

©2025 WithoutBook