# Help with array

**URL:** <https://www.ruby-forum.com/t/help-with-array/136717>\
**Category:** Ruby\
**Created:** [May 5, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/help-with-array/136717 "2008-05-05T21:07:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [May 5, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/help-with-array/136717/1 "2008-05-05T21:07:26Z")

</div>

Team,

On my never ending saga to learn Ruby, I am trying to write a program to  
solve Sudoku puzzles.  
Given a multidimensional array (9x9) I am trying to inspect each row and  
each column as follows:

Assume the following represents a typical row in my 9x9 array:

_Row_\*\*

23

689

7

12345689

1359

9

2468

359

235  
Row  
23  
689  
7  
12345689  
1359  
9  
2468  
359  
235  
Row

23

689

7

12345689

1359

9

2468

359

235

Row  
23  
689  
7  
12345689  
1359  
9  
2468  
359  
235  
Furthermore, assume the following is a typical column:

_Column_\*\*

23

689

7

12345689

1359

9

2468

359

235  
I would like to be able to count the frequency of each digit on either  
row  
or column, separate of course!

_Number_\*\*

1

2

3

4

5

6

7

8

9

_Frequency_\*\*

2

4

5

2

4

3

1

3

5

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.

Any help will be greatly appreciated!

Thank you

Victor

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [May 6, 2008, 1:16am UTC](https://www.ruby-forum.com/t/help-with-array/136717/2 "2008-05-06T01:16:06Z")

</div>

Axel,

I will do so.

Thank you

Victor

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [May 5, 2008, 9:40pm UTC](https://www.ruby-forum.com/t/help-with-array/136717/3 "2008-05-05T21:40:36Z")

</div>

-------- Original-Nachricht --------

> Datum: Tue, 6 May 2008 04:06:01 +0900  
> Von: “Victor R.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> An: removed\_email\_address@domain.invalid  
> Betreff: Help with array

> 9  
> 12345689
> 
> 359  
> 2468  
> 7
> 
> 8
> 
> 5
> 
> Victor

Dear Victor,

one nice feature of the Ruby list is its quiz contest.  
You can have a look at different solution proposals for solving  
Sudokus here:

[http://www.rubyquiz.com/quiz43.html](http://www.rubyquiz.com/quiz43.html)

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [May 6, 2008, 5:40am UTC](https://www.ruby-forum.com/t/help-with-array/136717/4 "2008-05-06T05:40:40Z")

</div>

On May 5, 1:06 pm, Victor R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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=\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