Python Program To Find Subsets Of A Set
Chapter:
Python
Last Updated:
04-10-2023 17:47:40 UTC
Program:
/* ............... START ............... */
def find_subsets(input_set):
def generate_subsets(subset, index):
if index == len(input_set):
subsets.append(subset[:])
return
# Include the current element in the subset
subset.append(input_set[index])
generate_subsets(subset, index + 1)
# Exclude the current element from the subset
subset.pop()
generate_subsets(subset, index + 1)
subsets = []
generate_subsets([], 0)
return subsets
# Example usage:
input_set = [1, 2, 3]
result = find_subsets(input_set)
print("Subsets of", input_set, "are:")
for subset in result:
print(subset)
/* ............... END ............... */
Notes:
-
This program defines a function find_subsets that generates all subsets of the input set by using a recursive helper function generate_subsets. The subsets are collected in the subsets list, and the final result is returned.
- When you run this program with input_set = [1, 2, 3], it will print all the subsets of the set [1, 2, 3], including the empty set and the set itself.