給定多個數字 n = 1, 2, 3, 4, 5, 6。
我想生成一個嵌套陣列 S,它將包含 n 的所有可能的 4 位排列。由于 6^4 = 1296,將有 1296 種可能的排列。
示例:S = [[1,1,1,1],[1,1,1,2],[1,1,2,2]...[6,6,6,6]]
我使用值為 [1,1,1,1] 的第一個索引開始嵌套回圈,然后使用范圍為 0..1295 的 for in 回圈并嘗試將 S[i] 的值結轉到 S[i 1] 然后增加 S[i 1][x] 的值,其中 x 始終從 3 開始,然后遞減,直到達到 0,然后再次變為 3。我的程式的問題是當我嘗試增加 S[i 1][x] 時,S[i] 也會增加它的 S[i][x]。在下面的代碼中,S 被稱為“all_possible_combinations”
all_possible_combinations = Array.new(1296) {Array.new(4)}
all_possible_combinations[0] = [1, 1 ,1 ,1]
x = 3
for i in 0..1295
if i 1 == 1296
break
else
all_possible_combinations[i 1] = all_possible_combinations[i]
all_possible_combinations[i 1][x] = 1
x -= 1
if x == 0
x = 3
end
end
end
[附圖顯示了除錯程序,其中 S i][x] 也會增加
uj5u.com熱心網友回復:
您可以按如下方式計算該陣列。
a = [1, 2, 3, 4, 5, 6]
b = a.repeated_permutation(4).to_a
#=> [[1, 1, 1, 1], [1, 1, 1, 2], [1, 1, 1, 3], [1, 1, 1, 4], [1, 1, 1, 5],
# [1, 1, 1, 6], [1, 1, 2, 1], [1, 1, 2, 2], [1, 1, 2, 3], [1, 1, 2, 4],
# ...
# [6, 6, 5, 3], [6, 6, 5, 4], [6, 6, 5, 5], [6, 6, 5, 6], [6, 6, 6, 1],
# [6, 6, 6, 2], [6, 6, 6, 3], [6, 6, 6, 4], [6, 6, 6, 5], [6, 6, 6, 6]]
b.size
#=> 1296
見陣列#repeated_permutation
如果陣列a可能包含重復項,并且您希望洗掉重復的排列,您可能希望添加Array#uniq。
a = [1, 1, 3, 1, 1, 6]
b = a.repeated_permutation(4).to_a.uniq
#=> [[1, 1, 1, 1], [1, 1, 1, 3], [1, 1, 1, 6], [1, 1, 3, 1],
# [1, 1, 3, 3], [1, 1, 3, 6], [1, 1, 6, 1], [1, 1, 6, 3],
# [1, 1, 6, 6], [1, 3, 1, 1], [1, 3, 1, 3], [1, 3, 1, 6],
# [1, 3, 3, 1], [1, 3, 3, 3], [1, 3, 3, 6], [1, 3, 6, 1],
# [1, 3, 6, 3], [1, 3, 6, 6], [1, 6, 1, 1], [1, 6, 1, 3],
# [1, 6, 1, 6], [1, 6, 3, 1], [1, 6, 3, 3], [1, 6, 3, 6],
# [1, 6, 6, 1], [1, 6, 6, 3], [1, 6, 6, 6], [3, 1, 1, 1],
# [3, 1, 1, 3], [3, 1, 1, 6], [3, 1, 3, 1], [3, 1, 3, 3],
# [3, 1, 3, 6], [3, 1, 6, 1], [3, 1, 6, 3], [3, 1, 6, 6],
# [3, 3, 1, 1], [3, 3, 1, 3], [3, 3, 1, 6], [3, 3, 3, 1],
# [3, 3, 3, 3], [3, 3, 3, 6], [3, 3, 6, 1], [3, 3, 6, 3],
# [3, 3, 6, 6], [3, 6, 1, 1], [3, 6, 1, 3], [3, 6, 1, 6],
# [3, 6, 3, 1], [3, 6, 3, 3], [3, 6, 3, 6], [3, 6, 6, 1],
# [3, 6, 6, 3], [3, 6, 6, 6], [6, 1, 1, 1], [6, 1, 1, 3],
# [6, 1, 1, 6], [6, 1, 3, 1], [6, 1, 3, 3], [6, 1, 3, 6],
# [6, 1, 6, 1], [6, 1, 6, 3], [6, 1, 6, 6], [6, 3, 1, 1],
# [6, 3, 1, 3], [6, 3, 1, 6], [6, 3, 3, 1], [6, 3, 3, 3],
# [6, 3, 3, 6], [6, 3, 6, 1], [6, 3, 6, 3], [6, 3, 6, 6],
# [6, 6, 1, 1], [6, 6, 1, 3], [6, 6, 1, 6], [6, 6, 3, 1],
# [6, 6, 3, 3], [6, 6, 3, 6], [6, 6, 6, 1], [6, 6, 6, 3],
# [6, 6, 6, 6]]
b.size
#=> 81
uj5u.com熱心網友回復:
要創建一個序列,其中每個元素都是基于前一個元素生成的Enumerator.produce,例如:
enum = Enumerator.produce([1, 1, 1, 1]) do |a, b, c, d|
d = 1 # ^^^^^^^^^^^^
# initial value
if d > 6
d = 1
c = 1
end
if c > 6
c = 1
b = 1
end
if b > 6
b = 1
a = 1
end
if a > 6
raise StopIteration # <- stops enumeration
end
[a, b, c, d] # <- return value = next value
end
我故意使示例保持簡單,對四個數字中的每一個使用一個顯式變數。你當然也可以有一個陣列并使用一個小回圈來處理增量/進位。
以上為您提供:
enum.count #=> 1296
enum.first(3) #=> [[1, 1, 1, 1], [1, 1, 1, 2], [1, 1, 1, 3]]
enum.to_a.last(3) #=> [[6, 6, 6, 4], [6, 6, 6, 5], [6, 6, 6, 6]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489089.html
標籤:红宝石
