Permutationen

Code-Stücke können hier veröffentlicht werden.
Antworten
hubertgrassmann
User
Beiträge: 61
Registriert: Montag 26. Dezember 2022, 14:53

Code: Alles auswählen

def find(v,s,z):
    i = len(z)-1
    while z[i] < z[v]:
        i -= 1
    return i
        
def nextperm(z, n):
    s = n
    while s > 0 and z[s-1] > z[s]:
        s -= 1
    v = s-1
    i = find(v,s,z)
    z[i],z[v] = z[v],z[i]
    ende = z[s:]
    neu = sorted(ende)
    z[s:] = neu
    return z[1]==0, z

def zeig(z):
    for i in range(1,len(z)):
        print(z[i],end = '')
    print()

def main(n):    
    z = []
    for i in range(0, n+1):
        z.append(i)
    b = False
    while not b:
        zeig(z)
        b, z = nextperm(z, n)

main(4)

Benutzeravatar
__blackjack__
User
Beiträge: 14324
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

Warum denn jetzt hier noch mal? Hatte hier ja schon etwas dazu geschrieben: viewtopic.php?t=56499
„Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.“ — Brian W. Kernighan
Antworten