Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--drb
--format documentation
Binary file added .swp
Binary file not shown.
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source 'https://rubygems.org'
ruby '2.0.0'

gem "rspec", "~> 2.14.1"
gem 'guard-spork'
gem 'terminal-notifier-guard'
55 changes: 55 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
GEM
remote: https://rubygems.org/
specs:
celluloid (0.15.2)
timers (~> 1.1.0)
childprocess (0.3.9)
ffi (~> 1.0, >= 1.0.11)
coderay (1.1.0)
diff-lcs (1.2.5)
ffi (1.9.3)
formatador (0.2.4)
guard (2.2.5)
formatador (>= 0.2.4)
listen (~> 2.1)
lumberjack (~> 1.0)
pry (>= 0.9.12)
thor (>= 0.18.1)
guard-spork (1.5.0)
childprocess (>= 0.2.3)
guard (>= 1.1)
spork (>= 0.8.4)
listen (2.4.0)
celluloid (>= 0.15.2)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
lumberjack (1.0.4)
method_source (0.8.2)
pry (0.9.12.4)
coderay (~> 1.0)
method_source (~> 0.8)
slop (~> 3.4)
rb-fsevent (0.9.3)
rb-inotify (0.9.3)
ffi (>= 0.5.0)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.7)
rspec-expectations (2.14.4)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.4)
slop (3.4.7)
spork (1.0.0rc4)
terminal-notifier-guard (1.5.3)
thor (0.18.1)
timers (1.1.0)

PLATFORMS
ruby

DEPENDENCIES
guard-spork
rspec (~> 2.14.1)
terminal-notifier-guard
14 changes: 14 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
watch('Gemfile')
watch('lib/sudoku_validator.rb')
watch('spec/sudoku_validator_spec.rb')
watch('spec/spec_helper.rb') { :rspec }

end

guard 'rspec', all_after_pass: false do
end

1 change: 1 addition & 0 deletions examples/one_row.sudoku
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8 5 9 |6 1 2 |4 3 7
1 change: 1 addition & 0 deletions examples/one_row_invalid.sudoku
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8 5 9 |6 1 2 |4 3 3
4 changes: 4 additions & 0 deletions examples/three_row.sudoku
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
8 5 9 |6 1 2 |4 3 7
7 2 3 |8 5 4 |1 6 9
1 6 4 |3 7 9 |5 2 8
------+------+------
4 changes: 4 additions & 0 deletions examples/three_row_incomplete.sudoku
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
8 5 . |. . 2 |4 . .
7 2 3 |8 5 4 |1 6 9
1 6 4 |3 7 9 |5 2 8
------+------+------
4 changes: 4 additions & 0 deletions examples/three_row_invalid.sudoku
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
8 5 9 |6 1 2 |4 3 7
7 2 3 |8 5 4 |1 6 9
1 6 4 |3 7 9 |5 2 2
------+------+------
2 changes: 2 additions & 0 deletions examples/two_row.sudoku
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
8 5 9 |6 1 2 |4 3 7
7 2 3 |8 5 4 |1 6 9
24 changes: 24 additions & 0 deletions lib/sudoku_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class SudokuParser
def initialize(file)
@file = File.new(file, "r")
end

def parse
sudoku_array = []
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are both spaces and tabs used for indentation - it makes it a bit to make the review.

counter = 1
while (line = @file.gets)
next if line.index('-')
line = remove_bars(line)
sudoku_array << convert_string_to_array(line).collect{|i| i.to_i}
end
@file.close
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great that you remembered to close the file :)
However, if an error occurred in between the opening and closing it could be left opened, take a look at File.open method with block, which auto-closes the file.

sudoku_array
end

def remove_bars(line)
line.gsub('|', '')
end
def convert_string_to_array(string)
string.split(' ')
end
end
154 changes: 154 additions & 0 deletions lib/sudoku_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
class SudokuValidator
def initialize(sudoku = [])
@sudoku = sudoku
@rows = get_rows
@columns = get_columns
@grids = get_grids
end

def get_rows
@sudoku
end

def get_columns
@sudoku.transpose
end

def get_grids
a = @sudoku
grids = []
sub_grid = []

(0..2).each do |j|
(0..2).each do |i|
sub_grid << a[j][i]
end
end

grids << sub_grid
sub_grid = []
(0..2).each do |j|
(3..5).each do |i|
sub_grid << a[j][i]
end

end

grids << sub_grid
sub_grid = []
(0..2).each do |j|
(6..8).each do |i|
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a person who didn't write the code the ranges 0..2, 6..8 etc. are quite enigmatic. Perhaps you could introduce meaningful names for them

sub_grid << a[j][i]
end
end

grids << sub_grid
sub_grid = []

(3..5).each do |j|
(0..2).each do |i|
sub_grid << a[j][i]
end
end

grids << sub_grid
sub_grid = []
(3..5).each do |j|
(3..5).each do |i|
sub_grid << a[j][i]
end

end

grids << sub_grid
sub_grid = []
(3..5).each do |j|
(6..8).each do |i|
sub_grid << a[j][i]
end
end


sub_grid = []

(6..8).each do |j|
(0..2).each do |i|
sub_grid << a[j][i]
end
end

grids << sub_grid
sub_grid = []
(6..8).each do |j|
(3..5).each do |i|
sub_grid << a[j][i]
end

end

grids << sub_grid
sub_grid = []
(6..8).each do |j|
(6..8).each do |i|
sub_grid << a[j][i]
end
end

grids << sub_grid
grids
end

def validate_row(row)
row.collect{|i| i.to_i}.sort.join == "123456789"
end

def validate_column(column)
column.collect{|i| i.to_i}.sort.join == "123456789"
end

def validate_grid(grid)
grid.collect{|i| i.to_i}.sort.join == "123456789"
end


def validate_rows
@rows.each do |row|
if validate_row(row)
next
else
return false
end
return true
end
end

def validate_columns
@columns.each do |column|
if validate_column(column)
next
else
return false
end
return true
end
end

def validate_grids
@grids.each do |grid|
if validate_grid(grid)
next
else
return false
end
return true
end
end

def validate
if validate_rows && validate_columns && validate_grids
"This sudoku is valid."
else
"This sudoku is invalid."
end
end
end
1 change: 1 addition & 0 deletions one_row.sudoku
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8 5 9 |6 1 2 |4 3 7
15 changes: 15 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rubygems'
require 'spork'

Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require 'rspec/autorun'

RSpec.configure do |config|
end
end

Spork.each_run do
# This code will be run each time you run your specs.

end
57 changes: 57 additions & 0 deletions spec/sudoku_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'spec_helper'
require_relative '../lib/sudoku_parser'
describe SudokuParser do
context "#parse" do
it "returns the one row sudoku in an array" do
parser = SudokuParser.new('examples/one_row.sudoku')
result = parser.parse
result.should eq([[8, 5, 9, 6, 1, 2, 4, 3, 7 ]])
end

it "returns the two sudoku in an array" do
parser = SudokuParser.new('examples/two_row.sudoku')
result = parser.parse
result.should eq([[8, 5, 9, 6, 1, 2, 4, 3, 7 ],[7 ,2, 3 ,8, 5, 4, 1, 6, 9]])
end

it "returns the three sudoku in an array" do
parser = SudokuParser.new('examples/three_row.sudoku')
result = parser.parse
result.should eq([[8,5, 9, 6, 1, 2, 4, 3, 7 ],[7 ,2, 3 ,8, 5, 4, 1, 6, 9],[1, 6, 4, 3, 7, 9, 5, 2, 8 ]])
end

it "returns the three incomplete row sudoku in an array by replacing empty values with zero" do
parser = SudokuParser.new('examples/three_row_incomplete.sudoku')
result = parser.parse
result.should eq([[8, 5, 0, 0, 0, 2, 4, 0, 0], [7, 2, 3, 8, 5, 4, 1, 6, 9], [1, 6, 4, 3, 7, 9, 5, 2, 8]])
end

it "returns the full sudoku in an array" do
parser = SudokuParser.new('valid_complete.sudoku')
result = parser.parse
result.should eq([[8, 5, 9, 6, 1, 2, 4, 3, 7],
[7, 2, 3, 8, 5, 4, 1, 6, 9],
[1, 6, 4, 3, 7, 9, 5, 2, 8],
[9, 8, 6, 1, 4, 7, 3, 5, 2],
[3, 7, 5, 2, 6, 8, 9, 1, 4],
[2, 4, 1, 5, 9, 3, 7, 8, 6],
[4, 3, 2, 9, 8, 1, 6, 7, 5],
[6, 1, 7, 4, 2, 5, 8, 9, 3],
[5, 9, 8, 7, 3, 6, 2, 4, 1]])
end

it "returns the full incomplete sudoku in an array" do
parser = SudokuParser.new('valid_incomplete.sudoku')
result = parser.parse
result.should eq([[8, 5, 0, 0, 0, 2, 4, 0, 0],
[7, 2, 0, 0, 0, 0, 0, 0, 9],
[0, 0, 4, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 7, 0, 0, 2],
[3, 0, 5, 0, 0, 0, 9, 0, 0],
[0, 4, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 8, 0, 0, 7, 0],
[0, 1, 7, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 3, 6, 0, 4, 0]])
end
end
end
Loading