deserty Posted January 24, 2012 Report Share Posted January 24, 2012 Care ma poate ajuta cu un script care sa-mi genereze de exemplu toate combinatile de 3 numere din 5 cifre sa spunem ?Sa spunem ca am :$numbers="1 3 4 5 9";Si sa-mi genereze toate combinatile posibile de cate trei numere din numerele din variabila.ex : 134, 131, 139 etc .As dori sa pot pune in variabila cate numere vreau de la 1 la 9 ( ex: 1 2 3, alt ex : 1 2 3 4 5 ) inclusiv toate numerele. Sper ca m-am exprimat corect. Quote Link to comment Share on other sites More sharing options...
Ellimist Posted January 24, 2012 Report Share Posted January 24, 2012 Ceea ce ai nevoie se numeste Backtracking. Quote Link to comment Share on other sites More sharing options...
deserty Posted January 24, 2012 Author Report Share Posted January 24, 2012 Se pare ca am pus problema gresit.Deci daca am numerele 1,2,3,4Vreau sa-mi genereze 123, 124, 134, 234.Deci sa nu se repete acceasi cifra de 2 ori in acelasi numar.Si nici 123 si 132 sau 134 si 143 etc.Practic sunt aceleasi numere doar scrise intr-o alta ordine. Quote Link to comment Share on other sites More sharing options...
Ellimist Posted January 24, 2012 Report Share Posted January 24, 2012 Si sa-mi genereze toate combinatile posibile de cate trei numere din numerele din variabila.ex : 134, 131, 139 etc .Nu am inteles un cacat din ce vrei. Si in exemplul dat de tine se repeta 1 in 131...si cum cacat 134 e acelasi lucru cu 143, dar in alta ordine, pai da, este, dar asta ineamna combinare.... Fi mai explicit. Quote Link to comment Share on other sites More sharing options...
deserty Posted January 24, 2012 Author Report Share Posted January 24, 2012 Am explicat in al doilea post ce vreau. Si am specificat ca in primul post am pus gresit problema.Nu stiu daca este exact combinare pentru ca nu vreau sa se repede acceasi cifra de 2 ori in acelasi numar.Am dat un exemplu cat se poate de explicit.Deci daca am numerele 1,2,3,4Vreau sa-mi genereze 123, 124, 134, 234. Quote Link to comment Share on other sites More sharing options...
ZeroCold Posted January 24, 2012 Report Share Posted January 24, 2012 (edited) Combinari de n nr luate cate k. O_oVezi asta: Backtracking, combinari// Acelasi lucru a postat si Ellimist. In ce limbaj vrei scriptul? Edited January 24, 2012 by ZeroCold Quote Link to comment Share on other sites More sharing options...
deserty Posted January 24, 2012 Author Report Share Posted January 24, 2012 ZeroCold in php te-as ruga daca ai timp. Quote Link to comment Share on other sites More sharing options...
cmiN Posted January 24, 2012 Report Share Posted January 24, 2012 (edited) Judecand dupa ultimul post sunt aranjamente de N luate cate K (N!/(N-K)!), fiindca vrea si 123 si 132 (a zis sa nu se repete cifra, atat), in cazul combinarilor astea 2 numere ar fi fost acelasi lucru, deoarece s-ar fi plecat mereu de la pozitia din dreapta ultimei alese. Prin combinatii se referea la numarul total de functii adica N^K unde pentru fiecare pozitie se alege un numar indiferent daca a mai fost ales sau nu si in cazul asta se repetau numere.nrs = sorted([1, 2, 3, 4])K = 3sol = []def gen(pos): if pos == K: print "".join(str(x) for x in sol) return for x in nrs: # si daca vrei aranjamente if x not in sol and (pos == 0 or x > sol[pos - 1]): # scoti and-ul sol.append(x) gen(pos + 1) sol.pop()gen(0) Edited January 24, 2012 by cmiN Quote Link to comment Share on other sites More sharing options...
deserty Posted January 24, 2012 Author Report Share Posted January 24, 2012 (edited) Tot genereaza si 123 si 132.Eu nu vreau asta.123,132,321,312,213,231 - sunt aceleasi numere scrise doar sub o alta forma.LE : Merge.Merci cmiN.Python nu stiu deloc, scriptul ruleaza, e ok. Insa as dori daca mi l-ar face cineva in php. Edited January 24, 2012 by wildchild Quote Link to comment Share on other sites More sharing options...
Birkoff Posted January 25, 2012 Report Share Posted January 25, 2012 uite asta vrei tu, ai acolo codul si merge si cu litere si cu numere... e util pentru atacuri bruteforce algorithm - How to generate all permutations of a string in PHP? - Stack Overflow Quote Link to comment Share on other sites More sharing options...