Created
March 7, 2020 12:37
-
-
Save hacknlove/84596ec89044e106393383e2a783ede2 to your computer and use it in GitHub Desktop.
Biyección entre los números naturales y el conjunto potencia de los números naturales
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def nextSet (s): | |
""" | |
Esta función obtiene el siguiente subconjunto al subconjunto dado | |
""" | |
decrement = len(s) - 1 | |
if decrement == -1: | |
return [0] | |
increment = decrement | |
while True: | |
increment -=1 | |
if increment < 0: | |
if decrement < 2: | |
return increaseLength(s) | |
decrement -= 1 | |
increment = decrement - 1 | |
if s[decrement] - 1 <= s[increment] + 1: | |
continue | |
if s[increment] + 1 == s[increment + 1]: | |
continue | |
if s[decrement] - 1 == s[decrement - 1]: | |
continue | |
s[decrement] -= 1 | |
s[increment] += 1 | |
return s | |
def increaseLength(s): | |
sums = sum(s) | |
size = len(s) | |
nextS = list(range(0, size)) | |
lastElem = sums - sum(nextS) | |
if (lastElem <= size - 1): | |
return [sums + 1] | |
nextS.append(lastElem) | |
return nextS | |
def orderOf(x): | |
""" | |
Esta función obtiene el ordinal que le corresponde a un subconjunto, iterando desde el conjunto vacío hasta el conjunto objetivo. | |
""" | |
i = 0 | |
y = [] | |
while y!=x: | |
i += 1 | |
y = nextSet(y) | |
return i | |
def subsetNumber(x): | |
""" | |
Esta función devuelve el subconjunto que le corresponde a un ordinal, iterando desde el conjunto vacío hasta el ordinal objetivo | |
""" | |
i = 0 | |
y = [] | |
while i != x: | |
i += 1 | |
y = nextSet(y) | |
return y | |
def firstN(x): | |
""" | |
Este función imprime los x primeros subconjuntos | |
""" | |
i = 0 | |
s = [] | |
while i != x: | |
print (i, ':', s) | |
s=nextSet(s) | |
i += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment