clonecq clonecq - 1 month ago 9
Ruby Question

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

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]]

Answer

TL;DR

Your code doesn't work because you're incorrectly comparing objects. You would be better served by using a different data structure. For example, using a Hash that keys off of your ball values simplifies things a lot.

Why Your Code Doesn't Work

Your current code doesn't work because the variable i in your code is an Integer, not an Array. As a result, the Array#delete method doesn't match anything when performing its equality check. Consider the following minimal example:

# 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
#=> []

Refactored Example

The following provides a method for storing matching draws into a Hash object. The hash keys are the possible ball values.

# 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

Sample Results

The Hash allows you to show all draws that contain a given ball value. Note that the value may be an empty array if no draws matched.

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

h[49]
#=> []