The big idea is that all anagrams are identical when sorted. So if you build a hash (don't know what Ruby calls these) of lists, where the keys are sorted words and the value is the list of words that sorts to the given key, then you can find anagrams very quickly by sorting the word and looking up in your hash.
rrenaud's answer is great, and here is an example of how to construct such a hash in ruby, given an array named "words" that contains all of the words in your dictionary:
@words_hash = words.each_with_object(Hash.new []) do |word, hash|
hash[word.chars.sort] += [word]
end
The code above assumes ruby 1.9.2. If you are using an older version then chars won't exist but you can use .split('').sort.
The default object of the hash is set to be the empty array, which makes the coding easier in some cases because you don't have to worry about the hash giving you nil.
def combine_anagrams(words)
output_array = Array.new(0)
words.each do |w1|
temp_array = []
words.each do |w2|
if (w2.downcase.split(//).sort == w1.downcase.split(//).sort)
temp_array.push(w2)
end
end
output_array.push(temp_array)
end
return output_array.uniq
end
One solution could be:
def combine_anagrams(words)
output_array = Array.new(0)
words.each do |w1|
temp_array = []
words.each do |w2|
if (w2.downcase.split(//).sort == w1.downcase.split(//).sort)
temp_array.push(w2)
end
end
output_array.push(temp_array)
end
return output_array.uniq
end
class String
def permutation(&block)
arr = split(//)
arr.permutation { |i| yield i.join }
end
end
wordlist = ["one", "two"]
"noe".permutation do |i|
puts "match found: #{i}" if wordlist.include?(i)
end
基本思想是它创建数组并使用它的排列函数来得出结果。它可能效率不高,但我发现它很优雅。 :D
I couldn't resist solving this ruby quiz :)
class String
def permutation(&block)
arr = split(//)
arr.permutation { |i| yield i.join }
end
end
wordlist = ["one", "two"]
"noe".permutation do |i|
puts "match found: #{i}" if wordlist.include?(i)
end
The basic idea is that it creates and array and uses it's permutation function to come up with the result. It may not be efficient but I find it elegant. :D
def anagrams(n)
text = File.open('dict.txt').read
candidates = []
text.each_line do |line|
if (line.length - 1) == n.length
candidates << line.gsub("\n",'')
end
end
result = []
candidates.each do |word|
if word.chars.sort == n.chars.sort
result << word
end
end
result
end
Here's quite similar of mine. Reading from a dictionary file and comparing sorted chars as an array. Sorting is done on preselected candidates.
def anagrams(n)
text = File.open('dict.txt').read
candidates = []
text.each_line do |line|
if (line.length - 1) == n.length
candidates << line.gsub("\n",'')
end
end
result = []
candidates.each do |word|
if word.chars.sort == n.chars.sort
result << word
end
end
result
end
发布评论
评论(7)
最重要的想法是,排序后所有字谜词都是相同的。因此,如果您构建一个列表的散列(不知道 Ruby 如何称呼这些),其中键是排序的单词,值是按给定键排序的单词列表,那么您可以通过对单词并在您的哈希中查找。
The big idea is that all anagrams are identical when sorted. So if you build a hash (don't know what Ruby calls these) of lists, where the keys are sorted words and the value is the list of words that sorts to the given key, then you can find anagrams very quickly by sorting the word and looking up in your hash.
rrenaud 的回答很好,下面是如何在 ruby 中构造这样的哈希的示例,给定一个名为“
words
”的数组,其中包含字典中的所有单词:上面的代码假设 ruby 1.9 .2.如果您使用的是旧版本,则
chars
将不存在,但您可以使用.split('').sort
。散列的默认对象设置为空数组,这使得在某些情况下编码更容易,因为您不必担心散列给您 nil。
资料来源: https://github.com/DavidEGrayson/anagram/blob/master/david .rb
rrenaud's answer is great, and here is an example of how to construct such a hash in ruby, given an array named "
words
" that contains all of the words in your dictionary:The code above assumes ruby 1.9.2. If you are using an older version then
chars
won't exist but you can use.split('').sort
.The default object of the hash is set to be the empty array, which makes the coding easier in some cases because you don't have to worry about the hash giving you nil.
Source: https://github.com/DavidEGrayson/anagram/blob/master/david.rb
一种解决方案可能是:
One solution could be:
我无法抗拒解决这个 ruby 测验:)
基本思想是它创建数组并使用它的排列函数来得出结果。它可能效率不高,但我发现它很优雅。 :D
I couldn't resist solving this ruby quiz :)
The basic idea is that it creates and array and uses it's permutation function to come up with the result. It may not be efficient but I find it elegant. :D
这可能就是您正在寻找的内容:解决字谜Ruby
这是另一种方法(这是顶部响应):Python 中的 Anagram 求解器
This might be what you're looking for: Solving Anagrams In Ruby
Here's another approach (it's the top response): Anagram Solver In Python
这里和我的很相似。从字典文件中读取并将排序后的字符作为数组进行比较。对预先选定的候选人进行排序。
Here's quite similar of mine. Reading from a dictionary file and comparing sorted chars as an array. Sorting is done on preselected candidates.