Created
March 3, 2019 05:37
-
-
Save RichardJordan/e71efe6298065d8886f796d26f06b4cd to your computer and use it in GitHub Desktop.
Interview Questions -- 2
This file contains 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
# This was a neat little question that fit into about a half our coderpad session. The question is below, with my solution below that: | |
# | |
# have a look at an example puzzle: https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Sudoku-by-L2G-20050714_solution.svg/250px-Sudoku-by-L2G-20050714_solution.svg.png | |
# | |
# There are 3 simple rules to solving a Sudoku puzzle. | |
# 1. Each row in the puzzle is exactly 1 through 9. No duplicates. | |
# 2. Each column in the puzzle is exactly 1 through 9. No duplicates. | |
# 3. Each 3x3 matrix in the puzzle is exactly 1 through 9. No duplicates. | |
# | |
# Part 1. | |
# Write a method that will validate the rows of the puzzle | |
# | |
# Part 2. | |
# Write a method that will validate the columns of the puzzle | |
# | |
# Part 3. | |
# Write a method that will validate the nine 3x3 matrixes of the puzzle | |
good_puzzle = [ | |
[8, 3, 5, 4, 1, 6, 9, 2, 7], | |
[2, 9, 6, 8, 5, 7, 4, 3, 1], | |
[4, 1, 7, 2, 9, 3, 6, 5, 8], | |
[5, 6, 9, 1, 3, 4, 7, 8, 2], | |
[1, 2, 3, 6, 7, 8, 5, 4, 9], | |
[7, 4, 8, 5, 2, 9, 1, 6, 3], | |
[6, 5, 2, 7, 8, 1, 3, 9, 4], | |
[9, 8, 1, 3, 4, 5, 2, 7, 6], | |
[3, 7, 4, 9, 6, 2, 8, 1, 5] | |
] | |
# | |
# My solution: | |
# | |
class Validatable | |
attr_reader :arr | |
def initialize(arr) | |
@arr = arr.flatten | |
end | |
def valid? | |
valid_size? && valid_contents? | |
end | |
private | |
def valid_contents? | |
(1..9).all? { |num| arr.include?(num) } | |
end | |
def valid_size? | |
arr.size == 9 | |
end | |
end | |
class Row < Validatable | |
end | |
class Column < Validatable | |
end | |
class Submatrix < Validatable | |
end | |
class Puzzle | |
attr_reader :puzzle_array | |
def initialize(puzzle_array) | |
@puzzle_array = puzzle_array | |
end | |
def valid? | |
validate(rows) && validate(columns) && validate(submatrices) | |
end | |
private | |
def build_columns | |
cols = Array.new(9) { Array.new } | |
puzzle_array.each do |line| | |
line.each_with_index do |num, index| | |
cols[index] << num | |
end | |
end | |
cols | |
end | |
def build_submatrices | |
subs = Array.new(9) { Array.new } | |
puzzle_array.slice(0,3).each do |line| | |
subs[0] << line.slice(0,3) | |
subs[1] << line.slice(3,3) | |
subs[2] << line.slice(6,3) | |
end | |
puzzle_array.slice(3,3).each do |line| | |
subs[3] << line.slice(0,3) | |
subs[4] << line.slice(3,3) | |
subs[5] << line.slice(6,3) | |
end | |
puzzle_array.slice(6,3).each do |line| | |
subs[6] << line.slice(0,3) | |
subs[7] << line.slice(3,3) | |
subs[8] << line.slice(6,3) | |
end | |
subs | |
end | |
def columns | |
@columns ||= build_columns.map { |col| Column.new(col) } | |
end | |
def rows | |
@rows ||= puzzle_array.map { |line| Row.new(line) } | |
end | |
def submatrices | |
@submatrices||= build_submatrices.map { |sub| Submatrix.new(sub) } | |
end | |
def validate(collection) | |
collection.all?(&:valid?) | |
end | |
end | |
$ puzzle = Puzzle.new(@good_puzzle) | |
$ puts puzzle.valid?.to_s | |
=> true | |
# The `def build_submatrices` method is a bit long and clunky. A possible refactor is below. | |
# It's shorter, but it's nothing like as readable. I'd probably leave the original as-is. | |
def build_submatrices | |
subs = Array.new(9) { Array.new } | |
[0,3,6].each do |start| | |
puzzle_array.slice(start, 3).each do |line| | |
[0,1,2].each do |num| | |
subs[start + num] << line.slice((num * 3), 3) | |
end | |
end | |
end | |
subs | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment