# Help with array

**URL:** <https://rubytalk.org/t/help-with-array/46293>\
**Category:** ruby-talk\
**Created:** [5 May 2008 19:06 UTC](https://rubytalk.org/t/help-with-array/46293 "2008-05-05T19:06:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Victor\_Reyes](https://avatars.discourse-cdn.com/v4/letter/v/ed655f/32.png) [@Victor\_Reyes](https://rubytalk.org/u/Victor_Reyes)\
**Post date:** [5 May 2008 19:06 UTC](https://rubytalk.org/t/help-with-array/46293/1 "2008-05-05T19:06:01Z")

</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:

&nbsp;&nbsp;\*Row\*\*\*

23

689

7

12345689

1359

9

2468

359

235  
&nbsp;&nbsp;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:

&nbsp;&nbsp;\*Column\*\*\*

23

689

7

12345689

1359

9

2468

359

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

&nbsp;&nbsp;\*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:** ![Axel\_Etzold](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@Axel\_Etzold](https://rubytalk.org/u/Axel_Etzold)\
**Post date:** [5 May 2008 19:39 UTC](https://rubytalk.org/t/help-with-array/46293/2 "2008-05-05T19:39:07Z")

</div>

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

> Datum: Tue, 6 May 2008 04:06:01 +0900  
> Von: "Victor Reyes" \<victor.reyes@gmail.com\>  
> An: ruby-talk@ruby-lang.org  
> Betreff: Help with array

> 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:
> 
> &nbsp;&nbsp;\*Row\*\*\*
> 
> 23
> 
> 689
> 
> 7
> 
> 12345689
> 
> 1359
> 
> 9
> 
> 2468
> 
> 359
> 
> 235  
> &nbsp;&nbsp;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:
> 
> &nbsp;&nbsp;\*Column\*\*\*
> 
> 23
> 
> 689
> 
> 7
> 
> 12345689
> 
> 1359
> 
> 9
> 
> 2468
> 
> 359
> 
> 235  
> &nbsp;&nbsp;I would like to be able to count the frequency of each digit on either  
> row  
> or column, separate of course!
> 
> &nbsp;&nbsp;\*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

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

> **···**
>
> --  
> Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!  
> Ideal für Modem und ISDN: [GMX Handytarife 2024 | jetzt wechseln & sparen](http://www.gmx.net/de/go/smartsurfer)

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Gavin\_Kistner3](https://rubytalk.org/u/Gavin_Kistner3)\
**Post date:** [6 May 2008 03:40 UTC](https://rubytalk.org/t/help-with-array/46293/3 "2008-05-06T03:40:05Z")

</div>

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){  
&nbsp;&nbsp;Array.new(9){ }  
}

# Fill each cell with dummy data (not sudoku-ready)  
# of zero or more integers  
cells.each{ |row|  
&nbsp;&nbsp;row.each{ |cell|  
&nbsp;&nbsp;&nbsp;&nbsp;1.upto(9){ |i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# 20% chance of each number in some cell leaves  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# us with a 13% chance of no numbers per cell.  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# There's thus a 99.999% chance that at least one  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# cell will be left empty (if my math is correct)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cell \<\< i if rand \< 0.2  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;}  
}

# 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|  
&nbsp;&nbsp;row.each\_with\_index{ |cell, col\_num|  
&nbsp;&nbsp;&nbsp;&nbsp;cell.each{ |num|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nums\_by\_row[row\_num][num] += 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nums\_by\_col[col\_num][num] += 1  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;}  
}

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

> **···**
>
> On May 5, 1:06 pm, Victor Reyes \<victor.re...@gmail.com\> 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.

---

<div class="post-metadata">

**Author:** ![Victor\_Reyes](https://avatars.discourse-cdn.com/v4/letter/v/ed655f/32.png) [@Victor\_Reyes](https://rubytalk.org/u/Victor_Reyes)\
**Post date:** [5 May 2008 23:15 UTC](https://rubytalk.org/t/help-with-array/46293/4 "2008-05-05T23:15:16Z")

</div>

Axel,

I will do so.

Thank you

Victor

> **···**
>
> On 5/5/08, Axel Etzold \<AEtzold@gmx.de\> wrote:
> 
> > -------- Original-Nachricht --------  
> > \> Datum: Tue, 6 May 2008 04:06:01 +0900  
> > \> Von: "Victor Reyes" \<victor.reyes@gmail.com\>  
> > \> An: ruby-talk@ruby-lang.org  
> > \> Betreff: Help with array
> > 
> > \> 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
> > 
> > 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:
> > 
> > [Ruby Quiz - Sodoku Solver (#43)](http://www.rubyquiz.com/quiz43.html)
> > 
> > Best regards,
> > 
> > Axel  
> > --  
> > Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!  
> > Ideal für Modem und ISDN: [http://www.gmx.net/de/go/smartsurfer](http://www.gmx.net/de/go/smartsurfer)
