From: Wyatt Greene Date: 2008-05-04T22:05:02+09:00 Subject: Re: Array combination check On May 4, 8:50 am, Nadim Kobeissi wrote: > Xavier Noria wrote: > > What is a combination? > > Please allow me to make myself more clear: > bunny=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] > cow="hello" > > I want to test all possible combinations of the items inside bunny (the > alphabet) starting from the smallest to the biggest (a, then b, then c.. > then aa, then ab, then ac, etc.) until I arrive to a case in which the > combination == cow, which means the combination would be "hello". > -- > Posted viahttp://www.ruby-forum.com/. You could try a brute-force algorithm, which uses loops to do exactly what you said: try "a" then "b" then "c", then try "aa", "ab", "ac"..., then "ba", "bb", etc. String has a succ method which would be useful for this. The problem is that the brute-force algorithm will be slow. For the example given, I think it will take around 26^5 (about 11 million) iterations before it matches on a string of size 5. If you want a faster algorithm, I would work backwards from the starting string "hello". Wyatt