From: Martin DeMello Date: 2007-01-11T23:25:32+09:00 Subject: Re: [QUIZ] Word Blender (#108) On 1/11/07, Fedor Labounko wrote: > 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. No, I do need the uniq since I want one prime per unique letter in the target word. I then calculate its signature by a loop over the entire word, not the reduced word, so "hell" is indeed matched for "hello" but not for, say, "whelps". martin