ich habe einen Permutationsalgorithmus geschrieben, der auf Zufallszahlen aufbaut; Nur zum Spaß.
Code: Alles auswählen
__author__ = 'Jan_PC'
import random as rd
from math import factorial
from time import clock
from itertools import permutations
def random_permutation(to_perm):
to_perm = list(to_perm)
out = []
while True:
store = []
while True:
item = rd.choice(input)
if item not in store and len(store) != len(to_perm):
store.append(item)
elif len(store) == len(to_perm):
break
if store not in out:
out.append(store)
if len(out) == factorial(len(to_perm)):
break
return out
a = clock()
print(random_permutation("ABC"))
b = clock()
meineversion = b-a
a = clock()
g = []
for i in (permutations("ABC")):
g.append(i)
print(g)
b = clock()
deineversion = b-a
print("Meine Version:", meineversion, "Deine Version: ", deineversion)
Meine Frage: Wie kann man iterativ einen "normalen" Algorithmus implementieren? Ich finde fast nur rekursive Ansätze.