From: "Simon Kröger" Date: 2005-08-26T07:13:07+09:00 Subject: Re: [SUMMARY] Sodoku Solver (#43) Adam Shelly wrote: > On 8/25/05, Ruby Quiz wrote: > >>I believe Adam Shelly's parse routine could benefit from similar simplifications >>if you want to try your own hand at a little refactoring. > > > Ok, I admit I'm a ruby newbie. But I'm trying to learn the Ruby Way. > How's this? > > Was: > > [...c-style ruby code...] > > Is: > def ParseBoard(file) > boardlist,size = [ ],0 > file.each do |line| > if line.delete!('^0-9A-Za-z_+') =~ /\++/ > @@boxcols= line.length-1 > else > boardlist += line.split(//).collect{|n| n.hex } > size = line.length > end > p line > end > boardlist,size > end > > The only difference is it adds 0's to the boardlist where it used to > add -1's, but the solver constructor handles anything outside 1..size > the same, so it doesn't matter. > It doesn't handle malformed input well, but neither did the old > version. That's for another exercise, maybe. > > -Adam much better! i have another version for you: def parse_board(file) @@boxcols= (s = file.read).scan(/[+-]+/).size() -1 boardlist = s.scan(/[0-9A-Za-z_]/).map{|c| c.hex} return boardlist, Math.sqrt(boardlist.size).to_i end assuming that boxcols is the number of boxes in horizontal or vertical direction, boardlist is one array (e.g. with 81 cells for a 9x9 board) and the second return parameter is the number of columns or rows. right? cheers Simon