我遍历列表的排列(18项)是这样的:红宝石枚举:立即跳过多次迭代(或者开始从n个迭代)
List = [item0..item18] # (unpredictable)
Permutation_size = 7
Start_at = 200_000_000
for item, i in List.repeated_permutation(Permutation_size).each_with_index
next if i < Start_at
# do stuff
end
Start_at
用于从先前保存的状态恢复所以它总是不同的,但它几乎需要200s达到2亿,所以我想知道是否有更快的方法来跳过多个迭代或从迭代n开始(将枚举数转换为数组需要更长的时间)。如果不是的话,也会赞赏一种创建自定义repeated_permutation(n).each_with_index
(即以相同顺序产生结果)的方法。
随时给我重定向到一个现有的答案(我还没有发现任何)
PS。 (我所想出)
class Array
def rep_per_with_index len, start_at = 0
b = size
raise 'btl' if b > 36
counter = [0]*len
# counter = (start_at.to_s b).split('').map {|i| ''.include?(i) ? i.to_i : (i.ord - 87)} #this is weird, your way is way faster
start_at.to_s(b).chars.map {|i| i.to_i b}
counter.unshift *[0]*(len - counter.length)
counter.reverse!
i = start_at
Enumerator.new do |y|
loop do
y << [counter.reverse.map {|i| self[i]}, i]
i += 1
counter[0] += 1
counter.each_with_index do |v, i|
if v >= b
if i == len - 1
raise StopIteration
else
counter[i] = 0
counter[i + 1] += 1
end
else
break
end
end
end
end
end
end
什么是'list'的近似最大尺寸是多少? –
理想情况下,我想要一个通用的解决方案,但现在的大小总是18 –