This repository was archived by the owner on Jun 8, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 36
initial contest entry #5
Open
elubin
wants to merge
3
commits into
thoughtbot:master
Choose a base branch
from
elubin:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .idea |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| source 'https://rubygems.org' | ||
|
|
||
| gem 'nokogiri', '~> 1.6.0' | ||
|
|
||
| group :development, :test do | ||
| gem "minitest", "~> 5.0.7" | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| GEM | ||
| remote: https://rubygems.org/ | ||
| specs: | ||
| mini_portile (0.5.1) | ||
| minitest (5.0.8) | ||
| nokogiri (1.6.0) | ||
| mini_portile (~> 0.5.0) | ||
|
|
||
| PLATFORMS | ||
| ruby | ||
|
|
||
| DEPENDENCIES | ||
| minitest (~> 5.0.7) | ||
| nokogiri (~> 1.6.0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| require_relative 'lib/board_reader' | ||
| require_relative 'lib/board_validator' | ||
|
|
||
| raise 'Missing input file - SudokuValidator <filename>' if ARGV.length != 1 | ||
| board = Sudoku::BoardValidator.new(Sudoku::BoardReader.new(ARGV[0]).read) | ||
| puts board.valid_board? | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| module Sudoku | ||
| class BoardReader | ||
|
|
||
| def initialize(file) | ||
| @file = file | ||
| end | ||
|
|
||
| def read | ||
| board = [] | ||
| open(@file).each_with_index do |l,index| | ||
| board << l.chomp! | ||
| # could get more involved checking specific lines here like ---+---+--- | ||
| raise "Invalid board string [#{board[index]}]" if board[index].match(valid_chars) == nil | ||
| end | ||
| board | ||
| end | ||
|
|
||
| private | ||
| def valid_chars | ||
| /^[1-9|+.\-\s]+$/ | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| module Sudoku | ||
| class BoardValidator | ||
|
|
||
| def initialize( board_string ) | ||
| raise 'Invalid number of rows in board' if board_string.count != 11 | ||
|
|
||
| row_index = 0 # can't use _with_index because of line rows | ||
| board_string.each do |one_row| | ||
| one_row_number_array = one_row.gsub('|','').gsub('+','').gsub('-','').split | ||
| if one_row_number_array.count == 9 | ||
| init_one_row( row_index, one_row_number_array ) | ||
| row_index += 1 | ||
| else | ||
| raise 'Invalid Board' if one_row_number_array.count != 0 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def valid_board? | ||
| @rows.each {|row| return false if !array_unique?(row)} | ||
| @cols.each {|col| return false if !array_unique?(col)} | ||
| @grids.each {|grid| return false if !array_unique?(grid)} | ||
| true | ||
| end | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| private | ||
|
|
||
| def init_one_row( row_id, row_string ) | ||
| # store the row as a row | ||
| rows[row_id].concat(row_string) | ||
| # store each element of the array into the cols arrays | ||
| row_string.each_with_index {|x,i| cols[i] << x} | ||
| # pull off 3 at a top and store in the grids array | ||
| in_groups_of(row_string,3).each_with_index do |triplet,i| | ||
| grids[((row_id/3)*3)+i].concat(triplet) | ||
| end | ||
| end | ||
|
|
||
| def array_unique?(arr) | ||
| # clone the tested array because delete is destructive | ||
| _arr = arr.clone | ||
| _arr.delete('.') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you think that's cleaner than delete? |
||
| _arr.uniq.count == _arr.count | ||
| end | ||
|
|
||
| def rows | ||
| @rows ||= [[],[],[],[],[],[],[],[],[]] | ||
| end | ||
|
|
||
| def cols | ||
| @cols ||= [[],[],[],[],[],[],[],[],[]] | ||
| end | ||
|
|
||
| def grids | ||
| @grids ||= [[],[],[],[],[],[],[],[],[]] | ||
| end | ||
|
|
||
| def in_groups_of(array,number) | ||
| groups = [] | ||
| array.each_slice(number) {|group| groups << group} | ||
| groups | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| A B C |D E F |G H I | ||
| 7 2 3 |8 5 4 |1 6 9 | ||
| 1 6 4 |3 7 9 |5 2 8 | ||
| ------+------+------ | ||
| . . . |. . . |. . . | ||
| . . . |. . . |. . . | ||
| . . . |. . . |. . . | ||
| ------+------+------ | ||
| . . . |. . . |. . . | ||
| . . . |. . . |. . . | ||
| . . . |. . . |. . . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| require 'minitest' | ||
| require 'minitest/autorun' | ||
| require_relative '../lib/board_reader' | ||
|
|
||
|
|
||
| class TestMacbethAnalyzer < MiniTest::Test | ||
|
|
||
| describe '#read' do | ||
| def test_read_valid_board_returns_11_rows | ||
| array = Sudoku::BoardReader.new('./specs/valid_test_board.sudoku').read | ||
| assert_equal( array.count, 11 ) | ||
| end | ||
| def test_read_valid_board_returns_correct_row | ||
| array = Sudoku::BoardReader.new('./specs/valid_test_board.sudoku').read | ||
| assert_equal( "8 5 9 |6 1 2 |4 3 7", array[0] ) | ||
| end | ||
| def test_read_error_board | ||
| board_reader = Sudoku::BoardReader.new('./specs/invalid_error_board.sudoku') | ||
| #puts board_reader.read | ||
| assert_raises(RuntimeError) { board_reader.read } | ||
| end | ||
| end | ||
| end | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| require 'minitest' | ||
| require 'minitest/autorun' | ||
| require_relative '../lib/board_validator' | ||
| require 'minitest/autorun' | ||
|
|
||
| module MiniTest::Assertions | ||
| def assert_contains(string, substring) | ||
| assert string.include?(substring), "Expected #{string} to contain #{substring}" | ||
| end | ||
| end | ||
| String.infect_an_assertion :assert_contains, :must_contain, :only_one_argument | ||
|
|
||
|
|
||
|
|
||
| class TestMacbethAnalyzer < MiniTest::Test | ||
|
|
||
| describe '#initialize' do | ||
| def setup | ||
| @test_board = [ | ||
| "1 1 1 |1 1 1 |1 1 1", | ||
| "2 2 2 |2 2 2 |2 2 2", | ||
| "3 3 3 |3 3 3 |3 3 3", | ||
| "------+------+------", | ||
| "4 4 4 |4 4 4 |4 4 4", | ||
| "5 5 5 |5 5 5 |5 5 5", | ||
| "6 6 6 |6 6 6 |6 6 6", | ||
| "------+------+------", | ||
| "7 7 7 |7 7 7 |7 7 7", | ||
| "8 8 8 |8 8 8 |8 8 8", | ||
| "9 9 9 |9 9 9 |9 9 9" | ||
| ] | ||
| @exception_board = [ | ||
| " |1 1 1 |1 1 1", | ||
| "2 2 2 |2 2 2 |2 2 2", | ||
| "3 3 3 |3 3 3 |3 3 3", | ||
| "------+------+------", | ||
| "4 4 4 |4 4 4 |4 4 4", | ||
| "5 5 5 |5 5 5 |5 5 5", | ||
| "6 6 6 |6 6 6 |6 6 6", | ||
| "------+------+------", | ||
| "7 7 7 |7 7 7 |7 7 7", | ||
| "8 8 8 |8 8 8 |8 8 8", | ||
| "9 9 9 |9 9 9 |9 9 9" | ||
| ] | ||
| @board_not_complete = [ | ||
| "1 1 1 |1 1 1 |1 1 1", | ||
| "2 2 2 |2 2 2 |2 2 2", | ||
| "3 3 3 |3 3 3 |3 3 3", | ||
| "------+------+------", | ||
| ] | ||
|
|
||
| end | ||
| def test_valid_validator_input | ||
| board = Sudoku::BoardValidator.new @test_board | ||
| board.inspect.to_s.must_contain( | ||
| 'rows=[["1", "1", "1", "1", "1", "1", "1", "1", "1"], '+ | ||
| '["2", "2", "2", "2", "2", "2", "2", "2", "2"], '+ | ||
| '["3", "3", "3", "3", "3", "3", "3", "3", "3"], '+ | ||
| '["4", "4", "4", "4", "4", "4", "4", "4", "4"], '+ | ||
| '["5", "5", "5", "5", "5", "5", "5", "5", "5"], '+ | ||
| '["6", "6", "6", "6", "6", "6", "6", "6", "6"], '+ | ||
| '["7", "7", "7", "7", "7", "7", "7", "7", "7"], '+ | ||
| '["8", "8", "8", "8", "8", "8", "8", "8", "8"], '+ | ||
| '["9", "9", "9", "9", "9", "9", "9", "9", "9"]]' | ||
| ) | ||
|
|
||
| board.inspect.to_s.must_contain( | ||
| 'cols=[["1", "2", "3", "4", "5", "6", "7", "8", "9"], '+ | ||
| '["1", "2", "3", "4", "5", "6", "7", "8", "9"], '+ | ||
| '["1", "2", "3", "4", "5", "6", "7", "8", "9"], '+ | ||
| '["1", "2", "3", "4", "5", "6", "7", "8", "9"], '+ | ||
| '["1", "2", "3", "4", "5", "6", "7", "8", "9"], '+ | ||
| '["1", "2", "3", "4", "5", "6", "7", "8", "9"], '+ | ||
| '["1", "2", "3", "4", "5", "6", "7", "8", "9"], '+ | ||
| '["1", "2", "3", "4", "5", "6", "7", "8", "9"], '+ | ||
| '["1", "2", "3", "4", "5", "6", "7", "8", "9"]]' | ||
| ) | ||
| board.inspect.to_s.must_contain( | ||
| 'grids=[["1", "1", "1", "2", "2", "2", "3", "3", "3"], '+ | ||
| '["1", "1", "1", "2", "2", "2", "3", "3", "3"], '+ | ||
| '["1", "1", "1", "2", "2", "2", "3", "3", "3"], '+ | ||
| '["4", "4", "4", "5", "5", "5", "6", "6", "6"], '+ | ||
| '["4", "4", "4", "5", "5", "5", "6", "6", "6"], '+ | ||
| '["4", "4", "4", "5", "5", "5", "6", "6", "6"], '+ | ||
| '["7", "7", "7", "8", "8", "8", "9", "9", "9"], '+ | ||
| '["7", "7", "7", "8", "8", "8", "9", "9", "9"], '+ | ||
| '["7", "7", "7", "8", "8", "8", "9", "9", "9"]]' | ||
| ) | ||
| end | ||
| def test_incomplete_board | ||
| assert_raises(RuntimeError) { Sudoku::BoardValidator.new @board_not_complete } | ||
| end | ||
| def test_invalid_entries_board | ||
| assert_raises(RuntimeError) { Sudoku::BoardValidator.new @exception_board } | ||
| end | ||
| end | ||
| describe '#valid_board?' do | ||
| def setup | ||
| @valid_board = [ | ||
| "4 5 2 |6 1 8 |9 7 3", | ||
| "3 1 8 |2 7 9 |5 4 6", | ||
| "7 9 6 |4 5 3 |8 1 2", | ||
| "------+------+------", | ||
| "2 4 7 |8 9 6 |1 3 5", | ||
| "9 3 1 |5 4 7 |2 6 8", | ||
| "8 6 5 |1 3 2 |7 9 4", | ||
| "------+------+------", | ||
| "5 2 9 |7 6 4 |3 8 1", | ||
| "6 8 3 |9 2 1 |4 5 7", | ||
| "1 7 4 |3 8 5 |6 2 9", | ||
| ] | ||
| @blank_board = [ | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| "------+------+------", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| "------+------+------", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ] | ||
| @invalid_row = [ | ||
| "1 . . |1 . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| "------+------+------", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| "------+------+------", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ] | ||
| @invalid_col = [ | ||
| "1 . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| "------+------+------", | ||
| ". . . |. . . |. . .", | ||
| "1 . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| "------+------+------", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ] | ||
| @invalid_grid = [ | ||
| "1 . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ". . 1 |. . . |. . .", | ||
| "------+------+------", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| "------+------+------", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ". . . |. . . |. . .", | ||
| ] | ||
| end | ||
| def test_valid_board | ||
| assert_equal(Sudoku::BoardValidator.new(@valid_board).valid_board?, true ) | ||
| end | ||
| def test_valid_empty_board | ||
| assert_equal(Sudoku::BoardValidator.new(@blank_board).valid_board?, true ) | ||
| end | ||
| def test_invalid_row | ||
| assert_equal(Sudoku::BoardValidator.new(@invalid_row).valid_board?, false ) | ||
| end | ||
| def test_invalid_col | ||
| assert_equal(Sudoku::BoardValidator.new(@invalid_col).valid_board?, false ) | ||
| end | ||
| def test_invalid_grid | ||
| assert_equal(Sudoku::BoardValidator.new(@invalid_grid).valid_board?, false ) | ||
| end | ||
| end | ||
| end | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| 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 | ||
| ------+------+------ | ||
| . . . |. . . |. . . | ||
| . . . |. . . |. . . | ||
| . . . |. . . |. . . | ||
| ------+------+------ | ||
| . . . |. . . |. . . | ||
| . . . |. . . |. . . | ||
| . . . |. . . |. . . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| 8 5 9 |6 1 2 |4 3 7 | ||
| 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 | ||
| ------+------+------ | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider using this?