[#303658] Remove Parts of a String — Dan __ <major_general_joe@...>

Alright, this is probably a really simple question to answer, but I just

15 messages 2008/06/01

[#303876] deleting first line from a file — suresh <suresh.amritapuri@...>

Hi

16 messages 2008/06/03

[#303934] Module philosophy — "Leslie Viljoen" <leslieviljoen@...>

Sorry to beat a dead horse, but to confirm: the only way to mix a

12 messages 2008/06/04

[#303941] A complete guide for Ruby Progammers — Mc Mohd <mcmohd@...>

This tutorial gives you complete knowledge starting from basic to

23 messages 2008/06/04
[#303943] Re: A complete guide for Ruby Progammers — Davi Vidal <davividal@...> 2008/06/04

Em Wednesday 04 June 2008, Mc Mohd escreveu:

[#303945] Re: A complete guide for Ruby Progammers — Mc Mohd <mcmohd@...> 2008/06/04

Sorry pal missed to send URL. Its here:

[#303947] Re: A complete guide for Ruby Progammers — "Oscar Del Ben" <thehcdreamer@...> 2008/06/04

Thanks for your work ;)

[#303948] Re: A complete guide for Ruby Progammers — "Leslie Viljoen" <leslieviljoen@...> 2008/06/04

This tutorial looks strangely familiar!

[#303957] A crosspost from the Perl Community — Star Cross <starx@...>

All,

55 messages 2008/06/04
[#303983] Re: A crosspost from the Perl Community — David Masover <ninja@...> 2008/06/04

On Wednesday 04 June 2008 12:20:37 Star Cross wrote:

[#304212] Re: A crosspost from the Perl Community — Jenda Krynicky <jenda@...> 2008/06/06

David Masover wrote:

[#304303] Re: A crosspost from the Perl Community — David Masover <ninja@...> 2008/06/07

On Friday 06 June 2008 12:02:19 Jenda Krynicky wrote:

[#305043] Re: A crosspost from the Perl Community — Jenda Krynicky <jenda@...> 2008/06/13

David Masover wrote:

[#304075] Re: A crosspost from the Perl Community — Dave Bass <davebass@...> 2008/06/05

Coming to Ruby recently from Perl, these are my comments.

[#304084] Re: A crosspost from the Perl Community — "Eric Mahurin" <eric.mahurin@...> 2008/06/05

On Thu, Jun 5, 2008 at 10:00 AM, Dave Bass <davebass@musician.org> wrote:

[#304175] Re: A crosspost from the Perl Community — Dave Bass <davebass@...> 2008/06/06

Eric Mahurin wrote:

[#304217] Preferable Pairs (#165) — "Matthew Moss" <matthew.moss@...>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

17 messages 2008/06/06

[#304230] Desktop multi-plataform ruby app — "Israel Guerra" <israel.guerra@...>

Hail everyone!

60 messages 2008/06/06
[#304237] Re: Desktop multi-plataform ruby app — Charles Oliver Nutter <charles.nutter@...> 2008/06/06

Israel Guerra wrote:

[#304241] Re: Desktop multi-plataform ruby app — James Britt <james.britt@...> 2008/06/06

Charles Oliver Nutter wrote:

[#304242] Re: Desktop multi-plataform ruby app — "Israel Guerra" <israel.guerra@...> 2008/06/06

But guys, maybe im wrong about jruby, but its a ruby interpreter running in

[#304271] Re: Desktop multi-plataform ruby app — "Victor Reyes" <victor.reyes@...> 2008/06/07

On Fri, Jun 6, 2008 at 4:15 PM, Charles Oliver Nutter <

[#304240] Re: Desktop multi-plataform ruby app — James Britt <james.britt@...> 2008/06/06

Israel Guerra wrote:

[#304309] Re: Desktop multi-plataform ruby app — Tom Cloyd <tomcloyd@...> 2008/06/07

James Britt wrote:

[#304284] using portions of other methods in a new method — Jason Lillywhite <jason.lillywhite@...>

How do you take a piece of a method and use it in another? Here is my

10 messages 2008/06/07

[#304353] Ruby wishlist — jzakiya <jzakiya@...>

You can do this:

23 messages 2008/06/08

[#304443] my first program just shuttin' down — Ruby Noob <john_@...>

Why? I tryin' to open the "hello.rb" program, but it just shuttin' down

12 messages 2008/06/08

[#304541] Ruby vs JRuby Performance — "Victor Reyes" <victor.reyes@...>

I knew that there was a penalty to be paid when running JRuby, but I did not

20 messages 2008/06/09

[#304623] Random Number Stuff — David Stanislaus <stanislaus_d@...>

How would you create a random number generator thats limited to a

17 messages 2008/06/10

[#304640] accessing class variables from the outside (beginner question) — progcat@...

I am still learning Ruby and I am trying to get something

12 messages 2008/06/10

[#304662] webby, ubuntu and gems — "Martin DeMello" <martindemello@...>

I've recently switched distributions to ubuntu, and I'm having

15 messages 2008/06/10

[#304728] Basic Tree Data Structure — Justin To <tekmc@...>

class Tree

14 messages 2008/06/10
[#304738] Re: Basic Tree Data Structure — Justin To <tekmc@...> 2008/06/10

class Tree

[#304790] Trie data structure — Justin To <tekmc@...>

I'm trying to implement a trie data structure for my parsing program

17 messages 2008/06/10
[#304826] Re: Trie data structure — Dave Bass <davebass@...> 2008/06/11

Justin To wrote:

[#304825] each with else — Thorsten Mler <thorsten@80beans.com>

Hi all,

11 messages 2008/06/11

[#304875] write byte array to file — "Rajesh Olafson" <rolafson@...>

Helo

16 messages 2008/06/11

[#304960] Help with Ruby under cygwin — James Byrne <byrnejb@...>

In order to use git on my laptop (MS XPproSP3) I ended up installing the

23 messages 2008/06/12

[#304992] a simple patch for the ri utility — Daniel Choi <dhchoi@...>

Hi everyone

15 messages 2008/06/12

[#305021] array to string conversion — Clement Ow <clement.ow@...>

Hi, I have 2 arrays(which is part of the hash):

13 messages 2008/06/13

[#305058] Circle Drawing (#166) — "Matthew Moss" <matthew.moss@...>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

19 messages 2008/06/13

[#305100] Documenting Networking in Ruby. Any volunteer — "Victor Reyes" <victor.reyes@...>

Team,

10 messages 2008/06/13

[#305104] raise and rescue — Misiek Sz <nicpon@...>

Is is possible to raise an exception then rescue it and then go back to

13 messages 2008/06/13

[#305160] What was YOUR first Ruby Project — Eric Hegwer <ehegwer@...>

I though it be cool to hear what your first experience with Ruby was.

26 messages 2008/06/14

[#305227] why can't an instance instantiated within a class method access a protected instance method? — "Greg Hauptmann" <greg.hauptmann.ruby@...>

Hi,

10 messages 2008/06/15
[#305228] Re: why can't an instance instantiated within a class method access a protected instance method? — "Greg Hauptmann" <greg.hauptmann.ruby@...> 2008/06/15

(complete email)Hi,

[#305230] How !isset in Ruby — Alexey Tafintsev <alexey@...>

Hello people!

21 messages 2008/06/15
[#305238] Re: How !isset in Ruby — Martin Boese <boesemar@...> 2008/06/15

[#305241] Re: How !isset in Ruby — Robert Klemme <shortcutter@...> 2008/06/15

On 15.06.2008 14:37, Martin Boese wrote:

[#305268] little problem (google hiring puzzle) — ex <exeQtor@...>

Hi guys, I wonder if someone can find a pure ruby solution instead of

43 messages 2008/06/15

[#305334] How to Authenticate against the Windows NT Domain via Ruby — ChessMess <chessmess@...>

We are running a Rails application on Linux RedHat with a requirement

12 messages 2008/06/16

[#305377] print(true and true) #=> the parenthesis issue — hakunin <madfancier@...>

The parenthesis have been discussed before, but maybe this is another

31 messages 2008/06/17

[#305398] Can I find out the memory used by an object? — "Robert Hulme" <robert.hulme@...>

I'm 99% sure the answer to that question is no, but I thought I'd ask anyway :-)

15 messages 2008/06/17

[#305446] parsing text into usablle numerical data — Cthulhu __ <weedmasterp@...>

Hey total ruby n00b here...

13 messages 2008/06/17

[#305467] quick question about how array objects are handled — Chance Dinkins <chanceusc@...>

Btw, thanks in advance for any help - this community seems great!

11 messages 2008/06/17

[#305557] Rather validate values or use exceptions? — Joshua Muheim <forum@...>

Hi all

12 messages 2008/06/18

[#305605] Presentation on Ruby, require suggestions — "Srijayanth Sridhar" <srijayanth@...>

Hello,

14 messages 2008/06/19

[#305635] Why metaclasses? — "James Coglan" <jcoglan@...>

Hello all,

15 messages 2008/06/19

[#305727] Ruby 1.9.0/1.8.7/1.8.6/1.8.5 new releases (Security Fix) — Urabe Shyouhei <shyouhei@...>

Hi all.

91 messages 2008/06/20
[#305893] Re: Ruby 1.9.0/1.8.7/1.8.6/1.8.5 new releases (Security Fix) — Igal Koshevoy <igal@...> 2008/06/23

All versions of MRI Ruby that claim to fix the vulnerabilities are

[#305934] Re: Ruby 1.9.0/1.8.7/1.8.6/1.8.5 new releases (Security Fix) — Hongli Lai <hongli@...> 2008/06/23

Hi guys. Igal invited me to join this discussion.

[#305936] Re: Ruby 1.9.0/1.8.7/1.8.6/1.8.5 new releases (Security Fix) — Igal Koshevoy <igal@...> 2008/06/23

Hongli Lai wrote:

[#305943] Re: Ruby 1.9.0/1.8.7/1.8.6/1.8.5 new releases (Security Fix) — Hongli Lai <hongli@...> 2008/06/23

Igal Koshevoy wrote:

[#305956] Re: Ruby 1.9.0/1.8.7/1.8.6/1.8.5 new releases (Security Fix) — Igal Koshevoy <igal@...> 2008/06/23

Hongli Lai wrote:

[#306045] Re: Ruby 1.9.0/1.8.7/1.8.6/1.8.5 new releases (Security Fix) — Igal Koshevoy <igal@...> 2008/06/24

We have another potential winning solution!

[#306072] Re: Ruby 1.9.0/1.8.7/1.8.6/1.8.5 new releases (Security Fix) — Robert Thau <rst@...> 2008/06/24

Igal Koshevoy wrote:

[#306135] Re: Ruby 1.9.0/1.8.7/1.8.6/1.8.5 new releases (Security Fix) — Igal Koshevoy <igal@...> 2008/06/24

Robert Thau wrote:

[#306137] Re: Ruby 1.9.0/1.8.7/1.8.6/1.8.5 new releases (Security Fix) — "Dominic Sisneros" <dsisnero@...> 2008/06/25

Maybe you should try posting a issue on the new redmine bug tracker

[#306139] Re: Ruby 1.9.0/1.8.7/1.8.6/1.8.5 new releases (Security Fix) — Igal Koshevoy <igal@...> 2008/06/25

Dominic Sisneros wrote:

[#306214] Re: Ruby 1.9.0/1.8.7/1.8.6/1.8.5 new releases (Security Fix) — Jason Crystal <jcrystal@...> 2008/06/25

Just wanted to say that we all appreciate those fixes you guys have been

[#306516] Re: Ruby 1.9.0/1.8.7/1.8.6/1.8.5 new releases (Security Fix) — Cheri Anaclerio <canaclerio@...> 2008/06/29

Could somebody please explain how to apply the Smartleaf Stanislav and

[#307002] Re: Ruby 1.9.0/1.8.7/1.8.6/1.8.5 new releases (Security Fix) — Doug Alcorn <dougalcorn@...> 2008/07/02

Igal Koshevoy wrote:

[#305751] Regular expressions and long text — Guillermo.Acilu@...

Hello guys,

15 messages 2008/06/20

[#305810] Where does ruby excel? — Dolazy <francis.rammeloo@...>

I have only used ruby for writing little scripts. Things that are

14 messages 2008/06/21

[#305844] Initial release of amalgalite - v0.1.0 — Jeremy Hinegardner <jeremy@...>

Amalgalite embeds the SQLite database engine in a ruby extension.

13 messages 2008/06/21

[#305854] KABLAME! 0.2.1 Released — "Jacob Dunphy" <jacob.dunphy@...>

This is the first "announced release" of KABLAME!

13 messages 2008/06/22

[#305855] RubyGems 1.2.0 — Eric Hodel <drbrain@...7.net>

= Announce: RubyGems Release 1.2.0

49 messages 2008/06/22

[#305882] Is it ellegant to use a global variable to store a Logger object? — Iñaki Baz Castillo <ibc@...>

Hi, I use Logger class in a programm and since I need to log in lot of=20

49 messages 2008/06/22
[#305884] Re: Is it ellegant to use a global variable to store a Logger object? — Suraj Kurapati <snk@...> 2008/06/22

I単aki Baz Castillo wrote:

[#305885] Re: Is it ellegant to use a global variable to store a Logger object? — Iñaki Baz Castillo <ibc@...> 2008/06/22

El Lunes, 23 de Junio de 2008, Suraj Kurapati escribi=C3=B3:

[#305886] Re: Is it ellegant to use a global variable to store a Logger object? — Suraj Kurapati <snk@...> 2008/06/23

I単aki Baz Castillo wrote:

[#305888] Re: Is it ellegant to use a global variable to store a Logger object? — Iñaki Baz Castillo <ibc@...> 2008/06/23

El Lunes, 23 de Junio de 2008, Suraj Kurapati escribi=C3=B3:

[#306058] Re: Is it ellegant to use a global variable to store a Logger object? — Andrea Fazzi <andrea.fazzi@...> 2008/06/24

I単aki Baz Castillo ha scritto:

[#306066] Re: Is it ellegant to use a global variable to store a Logger object? — Iñaki Baz Castillo <ibc@...> 2008/06/24

El Martes, 24 de Junio de 2008, Andrea Fazzi escribi=C3=B3:

[#306161] Re: Is it ellegant to use a global variable to store a Logger object? — "Robert Klemme" <shortcutter@...> 2008/06/25

2008/6/24 I=F1aki Baz Castillo <ibc@aliax.net>:

[#306168] Re: Is it ellegant to use a global variable to store a Logger object? — "Robert Dober" <robert.dober@...> 2008/06/25

On Wed, Jun 25, 2008 at 11:22 AM, Robert Klemme

[#306176] Re: Is it ellegant to use a global variable to store a Logger object? — Andrea Fazzi <andrea.fazzi@...> 2008/06/25

Robert Dober ha scritto:

[#306180] Re: Is it ellegant to use a global variable to store a Logger object? — "Robert Dober" <robert.dober@...> 2008/06/25

On Wed, Jun 25, 2008 at 3:36 PM, Andrea Fazzi <andrea.fazzi@alca.le.it> wro=

[#306288] Re: Is it ellegant to use a global variable to store a Logger object? — "Shot (Piotr Szotkowski)" <shot@...> 2008/06/26

I=C3=B1aki Baz Castillo:

[#306351] Re: Is it ellegant to use a global variable to store a Logger object? — Iñaki Baz Castillo <ibc@...> 2008/06/26

El Jueves, 26 de Junio de 2008, Shot (Piotr Szotkowski) escribi=C3=B3:

[#306387] Re: Is it ellegant to use a global variable to store a Logger object? — "Shot (Piotr Szotkowski)" <shot@...> 2008/06/27

I=C3=B1aki Baz Castillo:

[#306416] Re: Is it ellegant to use a global variable to store a Logger object? — Iñaki Baz Castillo <ibc@...> 2008/06/27

El Viernes, 27 de Junio de 2008, Shot (Piotr Szotkowski) escribi=C3=B3:

[#306499] Re: Is it ellegant to use a global variable to store a Logger object? — "Shot (Piotr Szotkowski)" <shot@...> 2008/06/28

I=C3=B1aki Baz Castillo:

[#306501] Re: Is it ellegant to use a global variable to store a Logger object? — "ara.t.howard" <ara.t.howard@...> 2008/06/28

[#306085] Sequel primary keys — "Glen Holcomb" <damnbigman@...>

I posted to the Sequel Google Group but it's horribly slow, assuming it took

16 messages 2008/06/24

[#306088] Performance improvement possible? — Philip Rhoades <phil@...>

People,

35 messages 2008/06/24
[#306095] Re: Performance improvement possible? — Rob Biedenharn <Rob@...> 2008/06/24

On Jun 24, 2008, at 12:23 PM, Philip Rhoades wrote:

[#306225] Re: Performance improvement possible? — Philip Rhoades <phil@...> 2008/06/25

Rob,

[#306237] Re: Performance improvement possible? — Chuck Remes <cremes.devlist@...> 2008/06/26

[#306243] Re: Performance improvement possible? — Philip Rhoades <phil@...> 2008/06/26

Chuck,

[#306255] Re: Performance improvement possible? — Rob Biedenharn <Rob@...> 2008/06/26

On Jun 25, 2008, at 8:44 PM, Philip Rhoades wrote:

[#306333] Re: Performance improvement possible? — Eleanor McHugh <eleanor@...> 2008/06/26

On 26 Jun 2008, at 04:24, Rob Biedenharn wrote:

[#306345] Re: Performance improvement possible? — Philip Rhoades <phil@...> 2008/06/26

Eleanor,

[#306350] Re: Performance improvement possible? — Eleanor McHugh <eleanor@...> 2008/06/26

On 26 Jun 2008, at 20:47, Philip Rhoades wrote:

[#306357] Re: Performance improvement possible? — Philip Rhoades <phil@...> 2008/06/26

Ellie,

[#306368] Re: Performance improvement possible? — Eleanor McHugh <eleanor@...> 2008/06/27

On 26 Jun 2008, at 22:51, Philip Rhoades wrote:

[#306234] A cleaner way to pass a block or proc — "Tristin Davis" <tristin.colby@...>

Is there a cleaner way to implement my add_notifier method?

15 messages 2008/06/25
[#306236] Re: A cleaner way to pass a block or proc — Ben Bleything <ben@...> 2008/06/26

On Thu, Jun 26, 2008, Tristin Davis wrote:

[#306245] Re: A cleaner way to pass a block or proc — "Tristin Davis" <tristin.colby@...> 2008/06/26

Thanks Ben. That worked perfect. No other changes required in the class. :)

[#306331] question about defined? and y — Ruby Freak <twscannell@...>

The defined? keyword seems to have some funky behaviors.

19 messages 2008/06/26

[#306420] Statistician I (#167) — "Matthew Moss" <matthew.moss@...>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

13 messages 2008/06/27

[#306448] changing the shebang of ruby files best way ? — unbewusst.sein@... (Une B騅ue)

I've a lot of ruby files (grabed from net) having a wrong shebang for my

18 messages 2008/06/27
[#306505] Re: changing the shebang of ruby files best way ? — Ryan Davis <ryand-ruby@...> 2008/06/28

[#306524] Random Generation of Characters — Tj Superfly <nonstickglue@...>

How could you generate a list of all possible combination's of lowercase

12 messages 2008/06/29

[#306533] mysterious memory corruption, very confused — Seebs <usenet-nospam@...>

ruby 1.8.7-p22, OS X 10.4.mumble, PostgreSQL 8.3.1, ruby-pg 2008-03-18.

11 messages 2008/06/29

[#306547] Recursive Logic - Examples and Resources? — Dan __ <major_general_joe@...>

Hey all,

13 messages 2008/06/29

[#306620] Threads and Ruby — barjunk <barjunk@...>

I've been hunting around for information regarding threads, and to me,

35 messages 2008/06/30
[#306621] Re: Threads and Ruby — "ara.t.howard" <ara.t.howard@...> 2008/06/30

[#306701] nested methods good or bad design — John Maclean <jayeola@...>

=begin

15 messages 2008/06/30

[#306728] how to - quickly make permutations? — Max Williams <toastkid.williams@...>

can anyone provide an elegant implementation for this method?

18 messages 2008/06/30
[#306761] Re: how to - quickly make permutations? — Frederick Cheung <frederick.cheung@...> 2008/06/30

[#306778] Re: how to - quickly make permutations? — "jim finucane" <jimrails@...> 2008/07/01

each new element tries to double the size of the list

Re: Ruby vs JRuby Performance

From: Charles Oliver Nutter <charles.nutter@...>
Date: 2008-06-20 23:12:12 UTC
List: ruby-talk #305777
Victor Reyes wrote:
> The code is attached to this thread.
> However, I will send it to your email.
> Also, remember that this is code written by a Rubist wannabe and not
> necessary by one who knows the ins and outs of the language.
> I'll be interested in knowing what you find!

Ok, I managed to get it to run a few times in a cycle. First here's 
MRI's time for me:

   Elapsed time: around 0.191s

Now the time running "jruby sudoku01final.rb"

(all using JRuby trunk, "soylatte" Java 6 on OS X)

   Elapsed time: anywhere from 0.491s to 0.525s

If I specify --server, it degrades on this short run:

   Elapsed time: 1.6s to 1.65s

However, trying both runs ten times produces a very different result:

   Ten times without --server:
   Elapsed time (final result): 0.105s

   Ten times with --server:
   Elapsed time (final result): 0.212s

So there's some interesting slowdown for this short benchmark running 
under server. Let's try 100 iterations:

   100 times without --server:
   Elapsed time (final result): 0.103

   100 times with --server:
   Elapsed time (final result): 0.065s

It seems like the run is just too short for the server VM to really do 
much optimization, which is why we have JRuby run with the client VM by 
default.

I've attached the modified source.

- Charlie

Attachments (1)

sudoku01Final.rb (21.3 KB, text/x-ruby)
class Sudoku
	def initialize
    
    # This array contents, @ga, should be replaced with your puzzle or one of the 
    #  puzzles below.
     		@ga = [
        	[0,0,2,0,5,0,0,0,0],
        	[0,3,0,0,1,0,6,5,0],
        	[0,6,0,7,2,9,0,0,0],
        	[0,0,4,0,0,0,3,2,7],
        	[0,0,0,0,0,0,0,0,0],
        	[7,2,8,0,0,0,1,0,0],
        	[0,0,0,5,4,8,0,7,0],
        	[0,4,9,0,7,0,0,1,0],
        	[0,0,0,0,3,0,4,0,0]
       	]

# This is considered HARD
     #		@ga = [
     #   	[0,0,8,0,6,0,3,0,0],
      #  	[5,7,0,0,0,0,0,0,2],
       # 	[0,3,0,7,0,0,0,0,0],
       # 	[0,0,0,9,0,5,0,0,6],
       # 	[0,9,0,0,7,0,0,8,0],
      #  	[6,0,0,3,0,4,0,0,0],
      #  	[0,0,0,0,0,8,0,6,0],
      #  	[2,0,0,0,0,0,0,5,8],
      #  	[0,0,9,0,1,0,7,0,0]
       # 	]

     		@gam = [                                        # This is a mirror of @ga when first given.
        	[0,0,0,0,1,9,0,4,0],
        	[0,0,4,8,0,0,6,0,0],
        	[7,5,0,0,0,0,0,0,2],
        	[0,9,0,1,0,2,0,0,4],
        	[0,0,0,0,0,3,0,0,0],
        	[5,0,0,4,0,6,0,3,0],
        	[8,0,0,0,0,0,0,7,3],
        	[0,0,6,0,0,8,4,0,0],
        	[0,1,0,2,9,0,0,0,0]
        	]

#        @done = [
#        [2,1,8,4,9,7,5,3,6],
#        [3,6,9,8,2,5,1,4,7],
#        [7,5,4,1,6,3,9,8,2],
#        [8,2,6,3,5,4,7,1,9],
#        [9,3,5,2,7,1,8,6,4],
#        [4,7,1,9,8,6,3,2,5],
#        [1,9,3,5,4,2,6,7,8],
#        [5,4,7,6,1,8,2,9,3],
#        [6,8,2,7,3,9,4,5,1]
#        ]
   
    		# The following sudoku is from: http://www.life.com/Life/sudoku/0,26379,,00.html
    		# This puzzle is rated as: easy
	
     		@a = 	[
        	[0,0,2,0,5,0,0,0,0],
        	[0,3,0,0,1,0,6,5,0],
        	[0,6,0,7,2,9,0,0,0],
        	[0,0,4,0,0,0,3,2,7],
        	[0,0,0,0,0,0,0,0,0],
        	[7,2,8,0,0,0,1,0,0],
        	[0,0,0,5,4,8,0,7,0],
        	[0,4,9,0,7,0,0,1,0],
        	[0,0,0,0,3,0,4,0,0]
        	]
              
    		@ab = [
        	[0,0,0,0,1,0,0,0,0],
        	[0,0,6,0,0,8,9,0,0],
       	  [0,1,0,5,2,6,0,0,0],
        	[3,5,0,2,0,0,8,0,0],
        	[0,2,0,0,0,0,0,1,0],
        	[0,0,1,0,0,4,0,2,5],
        	[0,0,0,1,3,2,0,7,0],
        	[0,0,8,9,0,0,1,0,0],
        	[0,0,0,0,6,0,0,0,0]
          ]
           @pp = [
        	[0,1,8,0,9,0,5,3,0],
        	[0,0,9,0,2,0,0,4,7],
       	  [0,0,0,0,0,0,0,0,0],
        	[8,0,0,3,0,4,7,0,0],
        	[0,0,0,0,7,0,0,0,0],
        	[0,0,1,9,0,6,0,0,5],
        	[0,0,0,0,0,0,0,0,0],
        	[5,4,0,0,1,0,2,0,0],
        	[0,8,2,0,3,0,4,5,0]
            ]

        @aa = [
          [123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],
          [123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],
          [123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],
          [123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],
          [123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],
          [123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],
          [123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],
          [123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789],
          [123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789,123456789]
        ]
    
    		@elem  = Array.new
    		@tmp   = Array.new
        @index = Array.new
    
    		@elapsedTime = Time.now
    		puts @elapsedTime
    end # End of method initialize
      
##################################
# This method takes an integer as input.
# It returns an array of the digits found in the input.
#  For example, if the input is: 37615
# Th output will be the array [3,7,6,1,5]
###################################
  def int_split(x)
      r=[x];r[0,1]=*r[0].divmod(10)while r[0]>9;r
  end

##########################
# This method counts the frequency of a
# digit in the input array.
##########################
    def frequencyOfDigits(arrInput)
        h = Hash.new(0)
        arrInput.each {|x| h[x] += 1}
        return h
    end

	############################################
	# This method will set the initial board with the values provided by the user. #
	############################################
	def setInitialGrid
		for 
		   r in 0..8 do    	                  # These are the rows
       found = 0                          # When found = 1, found element, else row is all 0
       @tmp   = []
       @index = []
		   for
          c in 0..8 do                            # These are the colmns
              if @ga[r][c] == 0
                 @ga[r][c] = @aa[r][c]      # Push all possible values, 123456789, into array
              else
                 found = 1
                 @tmp.push@ga[r][c]       # @tmp contains given values for that row
                 @index.push c                # @index contains index of given values
              end
       end # End of for
            
            if found == 1
               max =  @tmp.size - 1                 # @tmp contains given values for the row
               (0..max).each do |v|                 # Loop through given values
                    (0..8).each do |cc|              # Number of elements per row = 9 (0..8)
                         newVal = @ga[r][cc]
                         newVal = eliminateDups(@ga[r][cc], @tmp[v])
                         @ga[r][cc] = newVal
                       #  puts "Received newVal: #{newVal}"
                    end      # End do
               end     # End do
            end # End if
  end # End outter for
          pGrid
          puts " "
  checkColumns        
end # End of method setInitialGrid
  
  ##################################
  # Now let's eliminate duplicate cells from the columns.     #
  ##################################  
   def checkColumns
     for
      c in 0..8 do                # The column will change slower
          for
             r in 0..8 do          # Columns                 
                   if @ga[r][c].to_i > 0 and  @ga[r][c].to_i < 10
                        element = @ga[r][c]
                     #   column = c                        
                         for
                             rr in 0..8 do
                               if @ga[rr][c].to_i < 10
                                   next
                               end
                               newVal = eliminateDups(@ga[rr][c], element)
                               @ga[rr][c] = newVal
					 newVal = checkForSingletons(@ga[rr])
					 @ga[rr]= newVal
                            end # End for
                    end  # End if
          end  # End inner loop
    end # End outter loop         

pGrid
puts " "
  end # End checkColumns
  
  ##################################
  # Now let's eliminate duplicate cells from the rows.         #
  ##################################  
   def checkRows
     for
      r in 0..8 do                
          for
             c in 0..8 do          # Columns                 
                   if @ga[r][c].to_i > 0 and  @ga[r][c].to_i < 10
                        element = @ga[r][c]
                        row = r                       
                         for
                             cc in 0..8 do
                               if @ga[r][cc].to_i < 10
                                   next
                               end
                               newVal = eliminateDups(@ga[r][cc], element)
                               @ga[r][cc] = newVal
                                newVal = checkForSingletons(@ga[r])
                                @ga[r]= newVal
                            end # End for
                    end  # End if
          end  # End inner loop
    end # End outter loop         

pGrid
puts " "
  end # End checkRows

##############################################
##############################################
# This methods, as the name implies, attempts to spot lone elements.
# This is accomplished by counting each element frequency in either a row, column
# or block. This method takes an array as input.
##############################################
##############################################
    def checkForSingletons(inA)
        savedinA = inA
        va = Array.new
        (0..8).each do |i|
              va = va + int_split(inA[i].to_i)          # This stmt passes one cell at a time to the splitter
        end  
              
        fredig = frequencyOfDigits(va)
        fredig.each_pair do |key, value|
            if value == 1             # If we found a singleton, isolate it.
                  (0..8).each do |k|
                       if savedinA[k].to_s.include?(key.to_s)
                             savedinA[k] = key
                       end # End if ...
                  end # End do
            end # if value
        end # End do
        
        return savedinA

    end # End method
##############################################
# This method scans input for duplicates. It will return a cell without duplicate.#
# @ga is the main array.                                                                              #
##############################################
    def driver
        controller = true
        while controller
              0.upto(49) do |n|
                    createBlocks
                    checkColumns
                    checkRows
                    return if checkSolution
               end # End do
        end
    end # End method driver

#################################################################
#################################################################
# This method will convert the 9x9 matrix into
# smaller 9 elements arrays. Each array represents
# a 3x3 block. The blocks are numbered from a to i.
#
# a, b, c
# d, e, f 
# g, h, i
#
# Block a =       x, y, z
#                     l, m, n
#                     o, p, q
# This method will ensure no dups are found, when appropriate.
# Also notice that all blocks arrays are created as global. That's to give
# full access to other methods without having to pass them as parameters.
#################################################################
#################################################################
     def createBlocks
          # Create block a
          j = 0
          @a = Array.new
          (0..2).each do |r|
              (0..2).each do |c|
                   @a[j] = @ga[r][c]
                   j += 1
              end  
          end
          
          ta = Array.new                # Temporary array
          ta = @a.find_all {|e| e.to_i <= 9}
           
          (0..8).each do |cell|
                ta.each do |elem|
                newCell = eliminateDups(@a[cell], elem)
                @a[cell] = newCell
                end  # End inner do loop
          end  # End outter do loop
           
          newa = checkForSingletons(@a)
          @a = newa

# Rebuild main array from block

          j = 0
          (0..2).each do |r|
              (0..2).each do |c|
                   @ga[r][c] = @a[j]
                   j += 1
              end  
          end

################################
          # Create block b
          j = 0
          @b = Array.new
          (0..2).each do |r|
              (3..5).each do |c|
                   @b[j] = @ga[r][c]
                   print @b[j].to_s + " "
                   j += 1
              end  
          end          

          ta = Array.new                # Temporary array
          ta = @b.find_all {|e| e.to_i <= 9}
           
          (0..8).each do |cell|
                ta.each do |elem|
                newCell = eliminateDups(@b[cell], elem)
                @b[cell] = newCell
                end  # End inner do loop
          end  # End outter do loop
           
          newb = checkForSingletons(@b)
          @b = newb
          
          # Rebuild main array from block
          j = 0
          (0..2).each do |r|
              (3..5).each do |c|
                   @ga[r][c] = @b[j]
                   j += 1
              end  
          end 
  
################################
          # Create block c
          j = 0
          @c = Array.new
          (0..2).each do |r|
              (6..8).each do |c|                
                   @c[j] = @ga[r][c]
                   j += 1
              end  
          end

          ta = Array.new                # Temporary array
          ta = @c.find_all {|e| e.to_i <= 9}
           
          (0..8).each do |cell|
                ta.each do |elem|
                newCell = eliminateDups(@c[cell], elem)
                @c[cell] = newCell
                end  # End inner do loop
          end  # End outter do loop
           
          newc = checkForSingletons(@c)
          @c = newc
          
          # Rebuild main array from block
          j = 0
          (0..2).each do |r|
              (6..8).each do |c|
                   @ga[r][c] = @c[j]
                   j += 1
              end  
          end
##################################
############  Next block of 3x3 ##
##################################
          # Create block d
          j = 0
          @d = Array.new
          (3..5).each do |r|
              (0..2).each do |c|
                   @d[j] = @ga[r][c]
                   j += 1
              end  
          end
          ta = Array.new                # Temporary array
          ta = @d.find_all {|e| e.to_i <= 9}
           
          (0..8).each do |cell|
                ta.each do |elem|
                newCell = eliminateDups(@d[cell], elem)
                @d[cell] = newCell
                end  # End inner do loop
          end  # End outter do loop
           
          newd = checkForSingletons(@d)
          @d = newd
          
          # Rebuild main array from block
          j = 0
          (3..5).each do |r|
              (0..2).each do |c|
                   @ga[r][c] = @d[j]
                   j += 1
              end  
          end
    
################################
          # Create block e
          j = 0
          @e = Array.new
          (3..5).each do |r|
              (3..5).each do |c|
                   @e[j] = @ga[r][c]
                   j += 1
              end  
          end          

          ta = Array.new                # Temporary array
          ta = @e.find_all {|e| e.to_i <= 9}
           
          (0..8).each do |cell|
                ta.each do |elem|
                newCell = eliminateDups(@e[cell], elem)
                @e[cell] = newCell
                end  # End inner do loop
          end  # End outter do loop
          
          newe = checkForSingletons(@e)
          @e = newe
          
          # Rebuild main array from block
          j = 0
          (3..5).each do |r|
              (3..5).each do |c|
                   @ga[r][c] = @e[j]
                   j += 1
              end  
          end
        
################################
          # Create block f
          j = 0
          @f = Array.new
          (3..5).each do |r|
              (6..8).each do |c|
                   @f[j] = @ga[r][c]
                   j += 1
              end  
          end          
  
          ta = Array.new                # Temporary array
          ta = @f.find_all {|e| e.to_i <= 9}
           
          (0..8).each do |cell|
                ta.each do |elem|
                newCell = eliminateDups(@f[cell], elem)
                @f[cell] = newCell
                end  # End inner do loop
          end  # End outter do loop
           
          newf = checkForSingletons(@f)
          @f = newf
          
          # Rebuild main array from block
          j = 0
          (3..5).each do |r|
              (6..8).each do |c|
                   @ga[r][c] = @f[j]
                    j += 1
              end  
           end
################################  
puts
##################################
############  Next block of 3x3 ##
##################################
          # Create block g
          j = 0
          @g = Array.new
          (6..8).each do |r|
              (0..2).each do |c|
                   @g[j] = @ga[r][c]
                   j += 1
              end  
          end
         
          ta = Array.new                # Temporary array
          ta = @g.find_all {|e| e.to_i <= 9}
           
          (0..8).each do |cell|
                ta.each do |elem|
                newCell = eliminateDups(@g[cell], elem)
                @g[cell] = newCell
                end  # End inner do loop
          end  # End outter do loop
                   
          newg = checkForSingletons(@g)
          @g = newg
          
          # Rebuild main array from block
          j = 0
          (6..8).each do |r|
              (0..2).each do |c|
                   @ga[r][c] = @g[j]
                   j += 1
              end  
           end

################################
          # Create block h
          j = 0
          @h = Array.new
          (6..8).each do |r|
              (3..5).each do |c|
                   @h[j] = @ga[r][c]
                    j += 1
              end  
          end          

          ta = Array.new                # Temporary array
          ta = @h.find_all {|e| e.to_i <= 9}
           
          (0..8).each do |cell|
                ta.each do |elem|
                newCell = eliminateDups(@h[cell], elem)
                @h[cell] = newCell
                end  # End inner do loop
          end  # End outter do loop
           
          newh = checkForSingletons(@h)
          @h = newh

# Rebuild main array from block
          j = 0
          (6..8).each do |r|
              (3..5).each do |c|
                   @ga[r][c] = @h[j]
                   j += 1
              end  
          end
################################
          j = 0
          @i = Array.new
          (6..8).each do |r|
              (6..8).each do |c|
                   @i[j] = @ga[r][c]
                   j += 1
              end  
          end          
         
          ta = Array.new                # Temporary array
          ta = @i.find_all {|e| e.to_i <= 9}
           
          (0..8).each do |cell|
                ta.each do |elem|
                newCell = eliminateDups(@i[cell], elem)
                @i[cell] = newCell
                end  # End inner do loop
          end  # End outter do loop
          
          newi = checkForSingletons(@i)
          @i = newi
          
          # Rebuild main array from block
          j = 0
          (6..8).each do |r|
              (6..8).each do |c|
                   @ga[r][c] = @i[j]
                   j += 1
              end  
           end
         pGrid
################################         
         
    end # End of createBlocks

	#########################################################
	# This method scans a cell removing dumplicate elements, after the initial board and after each find. 
  # cell represents one sudoku square, element is one of the given element.        
  # cell could be: 123456789 or less.            
  # element could be: Any single digit 1..9.
	#########################################################
    def eliminateDups(cell, element)          
          newStr = ""
          if cell.to_s.scan(element.to_s)                       # Does cell contains element?
                str = cell.to_s
                len = str.length
                if len == 1                                             # If length is 1, we have only one digit
                    return  cell
                end
          else
                return  cell                                            # Return to caller if value does not exist
          end
          
          (0..len-1).each do |f|
                if str[f,1] == element.to_s                         # If equal, then skip it and don't save it
                     next
                else
                     newStr = newStr + str[f,1].to_s             # Build new string without dup
                end                   
          end # End do
           
          return newStr
   end # End of method eliminateDups

	####################
	# Print current state of the grid #
	# The stmt: @ga[r][c]                #
	#  Where:                                 #
	#  r = row, c = column                 #
	####################
  	def pGrid
      	for 
        		r in 0..8 do
           		outR = ""
           		for
              		c in 0..8 do
                		outR = outR + @ga[r][c].to_s + " " 
           		end # End of inner for
              	puts outR
      	end # End outer for
    	end  # End of method pGrid
    
  	##########################
  	# Check all rows and columns                      #
  	##########################
  	def checkSolution
      	solved = 1              # Assume solved
  		# If any element is larger than > 9, puzzle is not solved!
      	(0..8).each do |r|
              if @ga[r].find {|v| v.to_i > 9}
                  solved = 0
              end
        end
               
      	if solved == 1
          		puts "SOLVED!"
          		pGrid
       #   		@@gcounter += 1
       #   		puts "Number of iterations: #{@@gcounter}"
          		@elapsedTime = Time.now - @elapsedTime
          		puts "Elapsed Time: #{@elapsedTime}"
          		true
      	end
  	end 
end # End of Class 

@@gcounter = 0		# Global variable @@gcounter

#######################
# This is the Genesis #
#######################
100.times {
ms = Sudoku.new

# Output initial settings
ms.pGrid 

# Set puzzle with given values
ms.setInitialGrid				# Apply the Given Numbers
ms.pGrid
ms.checkSolution
ms.driver
}

In This Thread

Prev Next