Counting values in an array, storing in a hash then making an arrayof hashes?

J

Jen

Hello,

Apologies but i've run in to another problem while attempting to make a
sudoku solver that uses basic genetic programming.

This time I'm trying to count how many times each number occures in each
board (boards stored in @offspring) then store the results in a hash.

I think each hash is being overwritten because the keys are the same, so
I'm attempting to store an array of hashes.

I think I'm close, but my logic is still a bit off, as the length of my
array of hashes is 1 but I expect it to be 4, with each element
containing a hash. 4 is the size of the test population I am using, so
there are 4 boards in @offspring.

I could have probably done the same thing with an array, but I confused
myself thinking about it :(

My code so far:

def fitness_check
hashes = Array.new
count = Hash.new 0
#Adds another loop to ensure all elements of offspring are checked
separetly.
i = 0
#loop through every row of offspring (first to fourth element)
@offspring[i.to_i].each do |r|
#loop through every column of every row
r.each do |c|
#Count the numbers in each column
count[c] += 1
hashes = count
end
end

#Need to stop the values in count getting overwritten by the next
element in @offspring
puts hashes.inspect
puts hashes.length
end

Thanks in advance for any suggestions.

Jen.
 
7

7stud --

totals = Hash.new(0)

arr = [1, 1, 2, 2, 3, 3, 1, 3]

arr.each do |val|
totals[val] += 1
end

p totals


--output:--
{1=>3, 2=>2, 3=>3}
 
7

7stud --

3)

totals = []

boards = [
[
[1,1,1],
[1,1,1],
[1,1,1]
],

[
[2,2,2],
[2,2,2],
[2,2,2]
]
]

board_totals = []

boards.each do |this_board|
this_board_totals = Hash.new(0)

this_board.each do |row|
row.each do |val|
this_board_totals[val] += 1
end
end

board_totals << this_board_totals
end

p board_totals

--output:--
[{1=>9}, {2=>9}]
 
R

Robert Klemme

Hello,

Apologies but i've run in to another problem while attempting to make a
sudoku solver that uses basic genetic programming.

This time I'm trying to count how many times each number occures in each
board (boards stored in @offspring) then store the results in a hash.

I think each hash is being overwritten because the keys are the same, so I'm
attempting to store an array of hashes.

I think I'm close, but my logic is still a bit off, as the length of my
array of hashes is 1 but I expect it to be 4, with each element containing a
hash. 4 is the size of the test population I am using, so there are 4 boards
in @offspring.

I could have probably done the same thing with an array, but I confused
myself thinking about it :(

My code so far:

def fitness_check
hashes = Array.new
count = Hash.new 0
#Adds another loop to ensure all elements of offspring are checked
separetly.
i = 0
#loop through every row of offspring (first to fourth element)
@offspring[i.to_i].each do |r|
#loop through every column of every row
r.each do |c|
#Count the numbers in each column
count[c] += 1
hashes = count
end
end

#Need to stop the values in count getting overwritten by the next element in
@offspring
puts hashes.inspect
puts hashes.length
end

Thanks in advance for any suggestions.


I'm not 100% positive that this is what you want, but it looks
suspiciously like what 7stud posted so here we go:

def fitness_check
# loop to ensure all elements of offspring are checked separetly.
hashes = @offspring.map do |board|
Hash.new(0).tap do |count|

# loop through every row of offspring (first to fourth element)
board.each do |r|
# loop through every column of every row
r.each do |c|
# Count the numbers in each column
count[c] += 1
end
end

count
end
end

# debug output
puts hashes.length
p hashes
end

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top