From: Phrogz Date: 2008-05-06T12:40:05+09:00 Subject: Re: Help with array On May 5, 1:06 pm, Victor Reyes wrote: > Given a multidimensional array (9x9) I am trying to inspect each row and > each column as follows: > I can do this with some ugly looking and probably inefficient loop. However, > Ruby has so many tools and "tricks" that perhaps there might be some way of > doing this quicker and easy to understand. I don't think there's any way to do this without looking at every cell. I was a little confused by your massive amount of text, so I assume you're saying that each cell has more than one number in it. (As occurs when writing down possibilities.) Following is a simple iteration that will go through every cell and, in a single pass, update the count of instances of a particular digit in each row and column. I've implemented the multiple-numbers-per-cell as an array per cell. If you have a string...well, don't do that, because it'd be silly. :) Hope this helps you with some insight into Ruby. As I'm offering this code up to a newbie to learn from, others please feel free to post followups ripping it apart and suggesting better ways to accomplish the same goals. # Create a 9x9 array, where each cell is an empty array cells = Array.new(9){ Array.new(9){ [] } } # Fill each cell with dummy data (not sudoku-ready) # of zero or more integers cells.each{ |row| row.each{ |cell| 1.upto(9){ |i| # 20% chance of each number in some cell leaves # us with a 13% chance of no numbers per cell. # There's thus a 99.999% chance that at least one # cell will be left empty (if my math is correct) cell << i if rand < 0.2 } } } # Now run through all the cells and count numbers nums_by_row = Array.new(9){ Hash.new(0) } nums_by_col = Array.new(9){ Hash.new(0) } cells.each_with_index{ |row, row_num| row.each_with_index{ |cell, col_num| cell.each{ |num| nums_by_row[ row_num ][ num ] += 1 nums_by_col[ col_num ][ num ] += 1 } } } # Following output hand-formatted for clarity # Beware unfortunate line-wrapping p cells # [ # [[1,3,6,7,9], [2,9], [1,8], [1,4], [1,6], [1], [7], [2,3,5,9], [8]], # [[2,3], [], [6,7], [5], [4,5,6,7,8,9], [2,5], [2,3,4], [7,9], []], # [[4], [3,4], [], [], [5,7], [1,2,5,6], [3,4], [2], [2,4,8]], # [[2,5], [7], [3,5,6,8,9], [], [3,7], [1,5,8], [1,2,4], [2], [3]], # [[4,5], [4,8], [6], [], [4], [], [9], [4,6], [7,8]], # [[3,4,6,9], [6], [9], [4,7], [2,9], [2,3,5,9], [4], [2,5,9], [9]], # [[8], [5], [2,7], [], [5], [6,7], [3,4,7,9], [4,5], [4,5,6]], # [[7,9], [], [], [2], [7,8,9], [3,7,9], [], [], [6,7]], # [[3], [5], [4,7], [1,2,5], [2,3,6,7,8], [1,3,5,8,9], [4,6], [], [5]] # ] p nums_by_row # [{1=>5, 2=>2, 3=>2, 4=>1, 5=>1, 6=>2, 7=>2, 8=>2, 9=>3}, # { 2=>3, 3=>2, 4=>2, 5=>3, 6=>2, 7=>3, 8=>1, 9=>2}, # {1=>1, 2=>3, 3=>2, 4=>4, 5=>2, 6=>1, 7=>1, 8=>1 }, # {1=>2, 2=>3, 3=>3, 4=>1, 5=>3, 6=>1, 7=>2, 8=>2, 9=>1}, # { 4=>4, 5=>1, 6=>2, 7=>1, 8=>2, 9=>1}, # { 2=>3, 3=>2, 4=>3, 5=>2, 6=>2, 7=>1, 9=>6}, # { 2=>1, 3=>1, 4=>3, 5=>4, 6=>2, 7=>3, 8=>1, 9=>1}, # { 2=>1, 3=>1, 6=>1, 7=>4, 8=>1, 9=>3}, # {1=>2, 2=>2, 3=>3, 4=>2, 5=>4, 6=>2, 7=>2, 8=>2, 9=>1}] p nums_by_col # [{1=>1, 2=>2, 3=>4, 4=>3, 5=>2, 6=>2, 7=>2, 8=>1, 9=>3}, #  { 2=>1, 3=>1, 4=>2, 5=>2, 6=>1, 7=>1, 8=>1, 9=>1}, #  {1=>1, 2=>1, 3=>1, 4=>1, 5=>1, 6=>3, 7=>3, 8=>2, 9=>2}, #  {1=>2, 2=>2, 4=>2, 5=>2, 7=>1 }, #  {1=>1, 2=>2, 3=>2, 4=>2, 5=>3, 6=>3, 7=>5, 8=>3, 9=>3}, #  {1=>4, 2=>3, 3=>3, 5=>5, 6=>2, 7=>2, 8=>2, 9=>3}, #  {1=>1, 2=>2, 3=>3, 4=>6, 6=>1, 7=>2, 9=>2}, #  {2=>4, 3=>1, 4=>2, 5=>3, 6=>1, 7=>1, 9=>3}, #  {2=>1, 3=>1, 4=>2, 5=>2, 6=>2, 7=>2, 8=>3, 9=>1}]