2
我的目標是轉換到b中:重新排序紅寶石陣列
a = [["a","b"], ["d", "c"], ["a", "o"], ["d", "g"], ["c", "a"]]
b = [[["a","b"], ["a", "o"]], ["c", "a"], [["d", "c"], ["d", "g"]]
它們是由每個嵌套陣列中的第一個元素進行分組。到目前爲止,我有:
def letter_frequency(c)
d = Hash.new(0)
c.each do |v|
d[v] += 1
end
d.each do |k, v|
end
end
def separate_arrays(arry)
arry2 = []
arry3 = []
big_arry = []
y = 0
while y < arry.length
arry2.push(arry[y][0])
arry3.push(arry[y][1])
y += 1
end
freq = letter_frequency(arry2)
front = arry.slice!(0..(freq["a"] - 1))
end
separate_arrays(a)
這不僅看起來矯枉過正,但現在有保證「一」將是一個合法的散列鍵,所以最後部分不工作。謝謝你的幫助。
完美,謝謝。 – EHNole 2012-03-05 10:38:41