Home > other >  How to iterate uneven arrays?
How to iterate uneven arrays?

Time:12-17

here i have two uneven arrays(size of array is not equal), i have to append insert when the arr1 numbers matches in arr2.but when condition become false the zero should only add one time instead of repeating. below is the code

arr2 =[     [[27, 28, 29, 30, 31, 1, 2], 5],
            [[31, 1, 2, 3, 4, 5, 6], 14],
            [[26, 27, 28, 29, 30, 31, 1], 22],
            [[28, 29, 30, 31, 1, 2, 3], 31], 
            [[25, 26, 27, 28, 29, 30, 31], 35],
            [[27, 28, 29, 30, 31, 1, 2], 44],
            [[29, 30, 31, 0, 0, 0, 0], 53] 
            ] 
arr1 = [5, 0, 14, 0, 22, 0, 31, 35, 0, 44, 0, 53] 

cc = []
arr1.each do|item_arr1|
    arr2.each_with_index do|item_arr2,index|
        if item_arr1 == arr2[index][-1] 
            cc << item_arr2
        else 
            cc << 0
        end 
    end 
end

output:

[
  [ [27, 28, 29, 30, 31, 1, 2], 5 ], 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 

  [ [31, 1, 2, 3, 4, 5, 6], 14 ], 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 

  [ [26, 27, 28, 29, 30, 31, 1], 22 ], 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 

  [ [28, 29, 30, 31, 1, 2, 3], 31 ], 
  0, 0, 0, 0, 0, 0, 0, 

  [ [25, 26, 27, 28, 29, 30, 31], 35 ], 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 

  [ [27, 28, 29, 30, 31, 1, 2], 44 ], 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 

  [ [29, 30, 31, 0, 0, 0, 0], 53 ]
]

Expected output:

[
  [ [27, 28, 29, 30, 31, 1, 2], 5 ], 0,

  [ [31, 1, 2, 3, 4, 5, 6], 14 ], 0, 

  [ [26, 27, 28, 29, 30, 31, 1], 22 ], 0, 

  [ [28, 29, 30, 31, 1, 2, 3], 31 ], 

  [ [25, 26, 27, 28, 29, 30, 31], 35 ], 0, 

  [ [27, 28, 29, 30, 31, 1, 2], 44 ], 0, 

  [ [29, 30, 31, 0, 0, 0, 0], 53 ]
]

CodePudding user response:

Assuming that the numbers are unique, you could use rassoc to fetch the sub-array from arr1:

arr1.map { |i| arr2.rassoc(i) || i }
#=> [
#     [[27, 28, 29, 30, 31, 1, 2], 5], 0,
#     [[31, 1, 2, 3, 4, 5, 6], 14], 0,
#     [[26, 27, 28, 29, 30, 31, 1], 22], 0,
#     [[28, 29, 30, 31, 1, 2, 3], 31],
#     [[25, 26, 27, 28, 29, 30, 31], 35], 0,
#     [[27, 28, 29, 30, 31, 1, 2], 44], 0,
#     [[29, 30, 31, 0, 0, 0, 0], 53]
#  ]

CodePudding user response:

This is not very Ruby-like but it should be relatively efficient because it avoids the need to search arrays.

a1 = arr1.size.times.with_object([]) do |i,a|
  n = arr1[i]
  next if n.zero?
  a << (i < arr1.size-1 && arr1[i 1].zero?) 
end 
  #=> [true, true, true, false, true, true, false]

A zero should be inserted after arr2[i] if a1[i] is true.

arr2.each_with_object([]) do |a2,a|
  a << a2
  a << 0 if a1.shift
end 
  #=> [
  #    [[27, 28, 29, 30, 31, 1, 2], 5], 0,
  #    [[31, 1, 2, 3, 4, 5, 6], 14], 0,
  #    [[26, 27, 28, 29, 30, 31, 1], 22], 0,
  #    [[28, 29, 30, 31, 1, 2, 3], 31],
  #    [[25, 26, 27, 28, 29, 30, 31], 35], 0,
  #    [[27, 28, 29, 30, 31, 1, 2], 44], 0,
  #    [[29, 30, 31, 0, 0, 0, 0], 53]
  #   ]
  •  Tags:  
  • ruby
  • Related