我有一個數組的數組。我試圖通過我的主數組中的每個數組,並比較索引的變量。如果它們大於我想從父數組中刪除該數組的變量。當我在比較b時運行下面的代碼時,它第一次運行並刪除數組,但似乎不再運行,並刪除索引> b的其餘數組。任何幫助讚賞。Ruby數組沒有循環兩次?
下面的函數應該打印14,但目前它的打印16
a = 4
b = 2
c = 5
n = 5
def count_configurations(a, b, c, n)
a = (0..a).to_a
b = (0..b).to_a
c = (0..c).to_a
big = [a.max, b.max, c.max].max
big = (0..big).to_a
arrays = big.repeated_permutation(3).to_a
solutions = []
arrays.each do |array|
sum = 0
array.each { |a| sum+=a }
if sum == n
solutions << array
end
end
solutions = solutions.uniq
solutions.each do |solution|
if solution[0] > a.max
solutions.delete(solution)
end
end
solutions.each do |solution|
if solution[1] > b.max
solutions.delete(solution)
end
end
solutions.each do |solution|
if solution[2] > c.max
solutions.delete(solution)
end
end
puts solutions.count
end
@texasbruce我是新的..所以我明白它可能不是最好的代碼。我會很感激,如果你可以添加一些有用的東西。 – jckly