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
My final solution to the SudokuValidator problem. #10
Open
charlieanna
wants to merge
5
commits into
thoughtbot:master
Choose a base branch
from
charlieanna: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
5 commits
Select commit
Hold shift + click to select a range
1ce2edc
Added SudokuParser for taking in the sudoku file and converting it in…
charlieanna 17bb8e4
Wrote the specs for sudoku_parser.rb
charlieanna f466c1b
Completed the validatiof compelete rows
charlieanna b5c274b
replacing the empty values in the sudoku with zeros
charlieanna 984a329
Completedmy solution to this problem.
charlieanna 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,3 @@ | ||
| --color | ||
| --drb | ||
| --format documentation |
Binary file not shown.
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,6 @@ | ||
| source 'https://rubygems.org' | ||
| ruby '2.0.0' | ||
|
|
||
| gem "rspec", "~> 2.14.1" | ||
| gem 'guard-spork' | ||
| gem 'terminal-notifier-guard' |
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,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 |
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 @@ | ||
| # 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 | ||
|
|
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 @@ | ||
| 8 5 9 |6 1 2 |4 3 7 |
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 @@ | ||
| 8 5 9 |6 1 2 |4 3 3 |
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,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 | ||
| ------+------+------ |
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,4 @@ | ||
| 8 5 . |. . 2 |4 . . | ||
| 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,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 | ||
| ------+------+------ |
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,2 @@ | ||
| 8 5 9 |6 1 2 |4 3 7 | ||
| 7 2 3 |8 5 4 |1 6 9 |
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 @@ | ||
| class SudokuParser | ||
| def initialize(file) | ||
| @file = File.new(file, "r") | ||
| end | ||
|
|
||
| def parse | ||
| sudoku_array = [] | ||
| 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 | ||
|
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. Great that you remembered to close the file :) |
||
| sudoku_array | ||
| end | ||
|
|
||
| def remove_bars(line) | ||
| line.gsub('|', '') | ||
| end | ||
| def convert_string_to_array(string) | ||
| string.split(' ') | ||
| 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,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| | ||
|
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. 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 | ||
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 @@ | ||
| 8 5 9 |6 1 2 |4 3 7 |
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,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 |
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,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 |
Oops, something went wrong.
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.
There are both spaces and tabs used for indentation - it makes it a bit to make the review.