For Programmers: Free Programming Magazines  


Home > Archive > Ruby > August 2005 > Re: [QUIZ] Sodoku Solver (#43) [SOLUTION]









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [QUIZ] Sodoku Solver (#43) [SOLUTION]
Gavin Kistner

2005-08-28, 6:59 pm

On Aug 23, 2005, at 8:49 PM, David Brady wrote:
> # Loads the @board array from a string matching the example above.
> def load(str)
> line_num = 0
> str.each_line do |line|
> line.gsub! '+', ''
> line.gsub! '-', ''
> line.gsub! '|', ''
> line.gsub! ' ', ' '
> line.gsub! '_', '0'
> line.strip!
> if line.length > 0
> l = line.split
> fail "Line length was #{l.length}" unless l.length == 9
> @board[line_num] = l.collect {|x| x.to_i}
> line_num += 1
> end
> end
>
> fail "Board is not valid." unless self.valid?
> end


How about just:
numbers = str.scan( /[\d_]/ ).collect{ |char| char.to_i }
and then check to see if you got 81 numbers or not (and split them up
into chunks of nine if you so desire).

If nothing else, how about :
line.gsub!( /[+| -]/, '' )



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com