我有一個類似的對象數組,其屬性值爲a
,其值可以是b
或c
。該數組可以視爲行的集合,其中數組中的每對項表示一行。我剛剛上市的屬性a
的數值爲簡單起見, 例子:Ruby數組迭代和變異
array = [c, b, b, c, c, c, b]
# array[0], array[1] is one row (c, b)
# array[2], array[3] is another (b, c)
# ...
不能有任何的只是(b, b)
行,如果是這樣的話,則b
值中的一個必須被交換爲數組中最接近的c
值。如果沒有更多c
值,則只要b
值保留在數組末尾,數組就會有效。
該最後陣列的行可以只包含一個值,即(b,)。
例子:
array = [b, c, b, b, c, b, b, b, c, b, b, c, c]
# becomes
array = [b, c, b, c, b, b, b, b, c, b, b, c, c]
array = [b, c, b, c, b, c, b, b, b, b, b, c, c]
array = [b, c, b, c, b, c, b, c, b, b, b, b, c]
array = [b, c, b, c, b, c, b, c, b, c, b, b, b]
# rows: (b, c), (b, c), (b, c), (b, c), (b, c), (b, b,), (b,)
這是我想到了,我真的不喜歡(因爲它是非常必要和詳細)解決方案
while true do
cand = nil
array.each_slice(2) do |item, nxt|
return if nxt.nil?
# pseudo-code: assume b? returns true for a == b
next unless item.b? && nxt.b?
cand = nxt
break
end
swap_cand = array.slice(array.index(cand), array.length).reject{ |item| item.popular? }.first
return if swap_cand.nil?
old_index, new_index = array.index(cand), array.index(swap_cand)
array[old_index], array[new_index] = array[new_index], array[old_index]
end
我一直運行到的一個問題在迭代時我無法改變數組,因此需要兩個循環。
編輯根據@ 7stud的建議清理了一些break語句。
'回做= true如果nxt.nil?'咦?你知道LocalJumpError是什麼嗎?如果你發佈的代碼實際上在def中,那麼設置done = true會爲你做什麼?當你從def返回時,沒有更多的循環 - 沒有更多的東西。 – 7stud 2014-09-22 19:15:28
我認爲這會簡單地設置'done'變量並隨後退出'each_slice'循環。這確實在一個函數定義中,並且如'until'循環所指定,當'done'爲'true'時,這個外部循環退出。 – nicohvi 2014-09-22 19:21:03
**我一直遇到的一個問題是,我無法在迭代它時對數組進行變異**。 'results = []; temp = []; arr.each do | obj |結果<< obj#或temp << obj。根據需要使用盡可能多的數組,並在每個循環中在它們之間來回拖放事物。 – 7stud 2014-09-22 19:22:31