Ruby unjumble单词和字典匹配

时间:2014-05-22 04:29:09

标签: ruby arrays string dictionary permutation

我正在尝试编写解密输入单词的ruby代码,然后将这些单词与字典匹配。目标是找到将这些单词与字典匹配并在字典中查找单词并打印出来。这就是我的代码的样子。

print "Enter scrambled word here:"
Jumble = gets.chomp

a = Jumble.split("")
perms = a.permutation.to_a.collect do |perm|
perm.join
end

return perms

fin = File.open("dict.txt", "r")

while line = fin.gets
word = line.chomp

if word = word.downcase
    perms = word
    print perms 
end

end

fin.close

我的代码似乎工作正常,直到“返回烫发”。它给我一个错误说意外返回(LocalJumpError)。我不知道在那一点之后我的代码是否正确,因为我无法超越这一点。我不知何故需要获取这些权限并将它们与字典匹配。需要打印字典中的第一个单词。我该怎么做呢

这就是字典的样子:

words_un
Aarhus
Aaron
Ababa
aback
abaft
abandon
abandoned
abandoning
abandonment
abandons
abase
...
Zulus
Zurich

我很感激反馈。

1 个答案:

答案 0 :(得分:0)

return perms

从方法或函数返回值(对象)。从技术上讲,你不是一个方法,所以Ruby不知道你在说什么。

额外:从技术上讲,你在Object类中,并且你不能从类中返回东西。