Ruby: 比较数组中的元素时出现 NoMethodError
Ruby: NoMethodError when comparing element in an array
我正在研究一种方法,该方法将一个单词数组作为参数,returns 一个数组数组,其中每个子数组包含彼此互为变位词的单词。 while sort_array[i][1]==temp do
行抛出 undefined method '[]' for nil:NilClass (NoMethodError)
,我不知道为什么。
def combine_anagrams(words)
sort_array = Hash.new
words.each do |w|
sort_array[w] = w.split("").sort
end
sort_array = sort_array.sort_by { |w, s| s }
return_array = []
i = 0
while i<sort_array.length do
temp_array = []
temp = sort_array[i][1]
while sort_array[i][1]==temp do
temp_array += [sort_array[i][0]]
i+=1
end
return_array += [temp_array]
end
return temp_array
end
p combine_anagrams( ['cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams','scream'] )
看起来这是因为您在递增 i
变量时没有检查以确保您仍在 sort_array
的范围内。要查看问题,请在代码中最内层的 while
循环中放置一个 puts
语句:
while sort_array[i][1]==temp do
temp_array += [sort_array[i][0]]
i+=1
puts "i is #{i} and the length is #{sort_array.length}"
end
然后 运行 你的代码,你会看到:
i is 1 and the length is 8
i is 2 and the length is 8
i is 3 and the length is 8
i is 4 and the length is 8
i is 5 and the length is 8
i is 6 and the length is 8
i is 7 and the length is 8
i is 8 and the length is 8
example.rb:15:in `combine_anagrams': undefined method `[]' for nil:NilClass (NoMethodError)
您需要确保在两个 while 循环中您都在数组的范围内,例如:
while i < sort_array.length && sort_array[i][1]==temp do
end
附带说明一下,您的代码目前只会 return 最后一个 temp_array
,因为您还在外部 while
循环的开头重置了它。而且,如果我了解您要解决的问题,您可能想查看 Array 上可用的 group_by
,这要归功于 Enumerable 模块:
words = ['cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams','scream']
words.group_by { |word| word.chars.sort }.values
# => [["cars", "racs", "scar"], ["for"], ["potatoes"], ["four"], ["creams", "scream"]]
我正在研究一种方法,该方法将一个单词数组作为参数,returns 一个数组数组,其中每个子数组包含彼此互为变位词的单词。 while sort_array[i][1]==temp do
行抛出 undefined method '[]' for nil:NilClass (NoMethodError)
,我不知道为什么。
def combine_anagrams(words)
sort_array = Hash.new
words.each do |w|
sort_array[w] = w.split("").sort
end
sort_array = sort_array.sort_by { |w, s| s }
return_array = []
i = 0
while i<sort_array.length do
temp_array = []
temp = sort_array[i][1]
while sort_array[i][1]==temp do
temp_array += [sort_array[i][0]]
i+=1
end
return_array += [temp_array]
end
return temp_array
end
p combine_anagrams( ['cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams','scream'] )
看起来这是因为您在递增 i
变量时没有检查以确保您仍在 sort_array
的范围内。要查看问题,请在代码中最内层的 while
循环中放置一个 puts
语句:
while sort_array[i][1]==temp do
temp_array += [sort_array[i][0]]
i+=1
puts "i is #{i} and the length is #{sort_array.length}"
end
然后 运行 你的代码,你会看到:
i is 1 and the length is 8
i is 2 and the length is 8
i is 3 and the length is 8
i is 4 and the length is 8
i is 5 and the length is 8
i is 6 and the length is 8
i is 7 and the length is 8
i is 8 and the length is 8
example.rb:15:in `combine_anagrams': undefined method `[]' for nil:NilClass (NoMethodError)
您需要确保在两个 while 循环中您都在数组的范围内,例如:
while i < sort_array.length && sort_array[i][1]==temp do
end
附带说明一下,您的代码目前只会 return 最后一个 temp_array
,因为您还在外部 while
循环的开头重置了它。而且,如果我了解您要解决的问题,您可能想查看 Array 上可用的 group_by
,这要归功于 Enumerable 模块:
words = ['cars', 'for', 'potatoes', 'racs', 'four','scar', 'creams','scream']
words.group_by { |word| word.chars.sort }.values
# => [["cars", "racs", "scar"], ["for"], ["potatoes"], ["four"], ["creams", "scream"]]