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

Discussion in 'Ruby' started by Jen, Mar 21, 2011.

  1. Jen

    Jen Guest

    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.
     
    Jen, Mar 21, 2011
    #1
    1. Advertising

  2. Jen

    7stud -- Guest

    Re: Counting values in an array, storing in a hash then making anarray of hashes?

    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}

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Mar 21, 2011
    #2
    1. Advertising

  3. Jen

    7stud -- Guest

    Re: Counting values in an array, storing in a hash then making anarray of hashes?

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

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Mar 22, 2011
    #3
  4. Re: Counting values in an array, storing in a hash then making anarray of hashes?

    On Mon, Mar 21, 2011 at 9:33 PM, Jen <> wrote:
    > 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

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Mar 23, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kazaam
    Replies:
    12
    Views:
    278
    Matthias Wächter
    Sep 13, 2007
  2. Matt Brooks
    Replies:
    16
    Views:
    228
    Matt Brooks
    Sep 16, 2009
  3. Tore Aursand
    Replies:
    3
    Views:
    557
    Anno Siegel
    Sep 16, 2003
  4. Scott  Gilpin
    Replies:
    2
    Views:
    221
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    215
Loading...

Share This Page