From: Robert Klemme Date: 2008-05-05T15:25:04+09:00 Subject: Re: Array combination check On 04.05.2008 14:50, 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". I am sorry, but this seems a rather silly approach. All you get as output is the information whether /cow/ can be built using characters in /bunny/. Your approach has at least O(n*n) while it seems there is a much simpler and faster (with O(n)) algorithm available: irb(main):001:0> require 'set' => true irb(main):002:0> bunny=("a".."z").to_set => # irb(main):003:0> cow="hello" => "hello" irb(main):004:0> def t(s,chars) irb(main):005:1> s.scan(/./) {|m| return false unless chars.include? m} irb(main):006:1> true irb(main):007:1> end => nil irb(main):008:0> t cow, bunny => true irb(main):009:0> t "___", bunny => false irb(main):010:0> Kind regards robert