From: Fedor Labounko Date: 2007-01-11T23:15:02+09:00 Subject: Re: [QUIZ] Word Blender (#108) ------=_Part_27239_31427289.1168524898147 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 1/11/07, Martin DeMello wrote: > > This just solves the find-all-subwords problem: > > target = ARGV[0] > dict = ARGV[1] || 'sowpods' > > reduced = target.split(//).sort.uniq.join > primes = [2, 3, 5, 7, 11, 13] > factors = [] > reduced.split(//).each_with_index {|e, i| > factors[e[0]] = primes[i] > } > > target_num = 1 > target.each_byte {|i| target_num *= factors[i]} > > IO.foreach(dict) {|word| > word.chomp! > next unless (word =~ /^[#{reduced}]+$/) && > (word.length < 7) && (word.length > 2) > p = 1 > word.each_byte {|i| p *= factors[i]} > puts word if target_num % p == 0 > } > > That's neat, and is a good general way of checking for inclusion (so you can extend it past characters in a string which might not have an .include? method). You probably don't want that .uniq in there though as that excludes you from matching 'hell' out of 'hello', for example. ------=_Part_27239_31427289.1168524898147--