Why doesn't Array#delete remove an element from my array of arrays?

时间:2016-10-15 17:23:00

标签: arrays ruby loops

To practice working with arrays and hashes I use a fun lotto app. Could someone please explain to me why result.delete(i) deleted the whole result array? Isn't is suppose to delete just THAT element and return the rest of the array?( using result - [i] works as intended):

for i in 1..number_of_balls do    # taking each possible ball
  results.each do |result|        # going through results
    if result.include?(i)         # find result that contains that ball
      #result.delete(i)           # delete itself from the result
      matched_numbers[i]<< result - [i] 
    end 
  end
end

Here are my inputs:

number_of_balls = 49 
results = [[3,11,12,14,41,43,13],[8,33,36,37,39,41,9], [1,6,23,24,27,39‌​,34],
           [3,9,10,13,20,4‌​3,34], [5,14,21,31,34‌​,47,45],[8,20,21,25,‌​31,41,33],
           [18,25,28,‌​33,36,42,7],[7,16,17‌​,31,40,48,26],[5,10,‌​23,27,37,38,33],
           [4,1‌​5,30,37,46,48,3], [7,‌​9,21,33,38,42,45], [1‌​1,17,19,20,36,43,9],‌​
           [7,14,17,20,37,47,34‌​],[25,28,29,30,35,44‌​,3],[8,18,36,39,41,4‌​7,31],
           [9,12,13,14,44‌​,48,18],[4,14,18,40,‌​43,44,5], [13,16,18,3‌​4,35,36,26],
           [11,23,2‌​5,28,29,36,27]]

Desired output:

    [[11, 12, 14, 41, 43, 13], [9, 10, 13, 20, 43, 34], [4, 15, 30, 37, 46, 48],
     [25, 28, 29, 30, 35, 44], [4, 19, 33, 34, 48, 39], [4, 9, 10, 11, 43, 46],
     [5, 6, 33, 38, 39, 8],    [2, 7, 21, 22, 30, 33],  [1, 19, 31, 32, 47, 37], 
     [17, 20, 30, 35, 48, 26]]

1 个答案:

答案 0 :(得分:2)

TL; DR

您的代码无法正常工作,因为您错误地比较了对象。使用不同的数据结构可以更好地满足您的需求。例如,使用哈希键来控制球的值可以简化很多事情。

为什么你的代码不起作用

您当前的代码不起作用,因为代码中的变量 i 是整数,而不是数组。因此,Array#delete方法在执行其相等性检查时不匹配任何内容。请考虑以下最小示例:

# Define a simplified array of arrays.
arrays = [[1, 2, 3]]
#=> [[1, 2, 3]]

# Doesn't delete anything because there's no match. This is correct
# behavior, since `1 == [1, 2, 3] #=> false`.
arrays.delete 1
#=> nil

# Deletes the specified array
arrays.delete [1, 2, 3]
#=> [1, 2, 3]

# Arrays is now empty, as expected.
arrays
#=> []

重构示例

以下提供了一种将匹配绘制存储到Hash对象中的方法。哈希键是可能的球值。

# Your sample data reformatted.
number_of_balls = 49 
results = [
  [3,11,12,14,41,43,13],
  [8,33,36,37,39,41,9],
  [1,6,23,24,27,39,34],
  [3,9,10,13,20,43,34],
  [5,14,21,31,34,47,45],
  [8,20,21,25,31,41,33],
  [18,25,28,33,36,42,7],
  [7,16,17,31,40,48,26],
  [5,10,23,27,37,38,33],
  [4,15,30,37,46,48,3],
  [7,9,21,33,38,42,45],
  [11,17,19,20,36,43,9],
  [7,14,17,20,37,47,34],
  [25,28,29,30,35,44,3],
  [8,18,36,39,41,47,31],
  [9,12,13,14,44,48,18],
  [4,14,18,40,43,44,5],
  [13,16,18,34,35,36,26],
  [11,23,25,28,29,36,27],
]

# Returns a Hash of draws matching each available ball.
def matched_draws avail_balls, seqs_drawn 
  draws = {}
  (1..avail_balls).each do |key|
     draws[key] = seqs_drawn.select { |ary| ary.include? key }
  end 
  draws
end

# Invoke using your existing variable names.
h = matched_draws number_of_balls, results

样本结果

哈希允许您显示包含给定球值的所有绘图。请注意,如果没有绘制匹配,则该值可能为空数组。

h[1]
#=> [[1, 6, 23, 24, 27, 39, 34]]

h[49]
#=> []
相关问题