Skip to content

Instantly share code, notes, and snippets.

@ChekeGT
Created April 2, 2024 09:02
Show Gist options
  • Save ChekeGT/22f03b006f7cf4dccf5048cc1922adbb to your computer and use it in GitHub Desktop.
Save ChekeGT/22f03b006f7cf4dccf5048cc1922adbb to your computer and use it in GitHub Desktop.
Funcion que retorna el conjunto potencia de un conjunto
def conjunto_potencia(conjunto):
conjunto_potencia = ["∅",conjunto.copy()]
def funcion_recursiva(conjunto, profundidad=0):
subconjuntos = []
for i in range(len(conjunto)):
subconjunto = conjunto.copy()
subconjunto.pop(i)
if subconjunto:
subconjuntos.append(subconjunto)
funcion_recursiva(subconjunto, profundidad + 1)
conjunto_potencia.extend(subconjuntos)
funcion_recursiva(conjunto)
# Limpiamos el conjunto de los pares repetidos
conjunto_limpio = []
for elemento in conjunto_potencia:
if elemento not in conjunto_limpio:
conjunto_limpio.append(elemento)
return conjunto_limpio
conjunto = [1,2,3, 4, 5]
resultado = conjunto_potencia(conjunto)
print(resultado)
print(len(resultado), 2 ** len(conjunto))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment