【发布时间】:2014-09-22 19:00:17
【问题描述】:
我有一个类似对象的数组,属性a 可以有值b 或c。可以将数组视为行的集合,其中数组中的每一对项目代表一行。为简单起见,我刚刚列出了属性 a 的值,
示例:
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 值留在数组末尾,数组就有效。
数组的 final 行可以只包含 一个 值,即。 e. (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
我一直遇到的一个问题是我无法在迭代数组时对其进行变异,因此需要两个循环。
edit根据@7stud 的建议清理了一些中断语句。
【问题讨论】:
-
return done = true if nxt.nil?嗯?你知道 LocalJumpError 是什么吗?如果您发布的代码实际上在 def 中,那么设置 done=true 对您有什么作用?当你从 def 返回时,没有更多的循环——没有更多的东西。 -
我假设这会简单地设置
done变量,然后退出each_slice循环。这确实在函数定义中,并且由until循环指定,当done为true时,此外部循环退出。 -
我一直遇到的一个问题是我无法在迭代数组时对其进行变异。 `结果 = [];温度 = []; arr.each 做 |obj|结果
-
@7stud 是的,我也这样做了,但这看起来和我最终得出的结果一样丑陋甚至更丑陋。如果您有更好的建议,我将不胜感激。
-
@7stud 你知道有两个循环,对吗?
标签: ruby