-
-
Save vincentchin/10f741eb674cf205f634 to your computer and use it in GitHub Desktop.
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
class Image | |
attr_accessor :array | |
def initialize(array) | |
@array = array | |
@cells = [] | |
@new_array = @array.map {|e| Array.new(e.size) } | |
end | |
def all_array_indexes | |
#Returns all possible array indexes within the size of the array | |
column_index = @array.first.size - 1 | |
row_index = @array.size - 1 | |
(0..row_index).to_a.product((0..column_index).to_a) | |
end | |
def manhattan_cells(x,y,n) | |
#Selects only cells within a manhattan distance of n relevant to a specified point | |
#Stores all cells into instance variable @cells | |
@cells = all_array_indexes.select { |i,j| ( (x-i).abs ) + ( (y-j).abs ) <= n } | |
end | |
def update_cells | |
#Goes through each value in @cells and plugs the row index (a) and column index (b) in @new_array[a][b] | |
@cells.uniq.each do |a,b| | |
@new_array[a][b] = 1 | |
end | |
end | |
def output_image | |
@array.each do |i| | |
puts i.join("") | |
end | |
end | |
def blur(n) | |
#Where it all comes together | |
@array.each_with_index do |row,x| | |
row.each_with_index do |cell,y| | |
if @array[x][y] == 1 | |
manhattan_cells(x,y,n) | |
update_cells | |
else | |
@new_array[x][y] ||= @array[x][y] | |
#Sets value to 0 in @new_array since it was also 0 in @array | |
end | |
end | |
end | |
return Image.new(@new_array).output_image | |
end | |
end | |
image = Image.new([ | |
[0, 0, 0, 0, 0, 0, 0, 0, 1], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 1, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 1], | |
]) | |
image.blur(4) |
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
require 'rspec' | |
require_relative 'imageblur' | |
describe Image do | |
describe '#blur' do | |
context 'with a 5 by 5 array and n of 2' do | |
it 'moves a manhattan distance of 2' do | |
image = Image.new([ | |
[0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0], | |
[0, 0, 1, 0, 0], | |
[0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0] | |
]) | |
new_image = Image.new([ | |
[0, 0, 1, 0, 0], | |
[0, 1, 1, 1, 0], | |
[1, 1, 1, 1, 1], | |
[0, 1, 1, 1, 0], | |
[0, 0, 1, 0, 0] | |
]) | |
expect(image.blur(2)).to eql(new_image.output_image) | |
end | |
end | |
context 'with a 9 by 12 array and n of 3' do | |
it 'moves a manhattan distance of 3' do | |
image = Image.new([ | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 1, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 1], | |
]) | |
new_image = Image.new([ | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 1, 0, 0, 0, 0], | |
[0, 0, 0, 1, 1, 1, 0, 0, 0], | |
[0, 0, 1, 1, 1, 1, 1, 0, 0], | |
[0, 1, 1, 1, 1, 1, 1, 1, 0], | |
[0, 0, 1, 1, 1, 1, 1, 0, 0], | |
[0, 0, 0, 1, 1, 1, 0, 0, 0], | |
[0, 0, 0, 0, 1, 0, 0, 0, 1], | |
[0, 0, 0, 0, 0, 0, 0, 1, 1], | |
[0, 0, 0, 0, 0, 0, 1, 1, 1], | |
[0, 0, 0, 0, 0, 1, 1, 1, 1], | |
]) | |
expect(image.blur(3)).to eql(new_image.output_image) | |
end | |
end | |
describe '#all array indexes' do | |
it 'returns all possible array indexes based on size of array' do | |
image = Image.new([ | |
[0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0], | |
[0, 0, 1, 0, 0], | |
[0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0] | |
]) | |
result = [ | |
[0, 0], [0, 1], [0, 2], [0, 3], | |
[0, 4], [1, 0], [1, 1], [1, 2], | |
[1, 3], [1, 4], [2, 0], [2, 1], | |
[2, 2], [2, 3], [2, 4], [3, 0], | |
[3, 1], [3, 2], [3, 3], [3, 4], | |
[4, 0], [4, 1], [4, 2], [4, 3], | |
[4, 4] | |
] | |
expect(image.all_array_indexes).to eql(result) | |
end | |
context 'when array is 12 by 9' do | |
it 'returns all possible array indexes in a 12 by 9 array' do | |
image = Image.new([ | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
]) | |
result = [ | |
[0, 0], [0, 1], [0, 2], [0, 3], | |
[0, 4], [0, 5], [0, 6], [0, 7], | |
[0, 8], [1, 0], [1, 1], [1, 2], [1, 3], | |
[1, 4], [1, 5], [1, 6], [1, 7], [1, 8], | |
[2, 0], [2, 1], [2, 2], [2, 3], [2, 4], | |
[2, 5], [2, 6], [2, 7], [2, 8], [3, 0], | |
[3, 1], [3, 2], [3, 3], [3, 4], [3, 5], | |
[3, 6], [3, 7], [3, 8], [4, 0], [4, 1], | |
[4, 2], [4, 3], [4, 4], [4, 5], [4, 6], | |
[4, 7], [4, 8], [5, 0], [5, 1], [5, 2], | |
[5, 3], [5, 4], [5, 5], [5, 6], [5, 7], | |
[5, 8], [6, 0], [6, 1], [6, 2], [6, 3], | |
[6, 4], [6, 5], [6, 6], [6, 7], [6, 8], | |
[7, 0], [7, 1], [7, 2], [7, 3], [7, 4], | |
[7, 5], [7, 6], [7, 7], [7, 8], [8, 0], | |
[8, 1], [8, 2], [8, 3], [8, 4], [8, 5], | |
[8, 6], [8, 7], [8, 8], [9, 0], [9, 1], | |
[9, 2], [9, 3], [9, 4], [9, 5], [9, 6], | |
[9, 7], [9, 8], [10, 0], [10, 1], [10, 2], | |
[10, 3], [10, 4], [10, 5], [10, 6], [10, 7], | |
[10, 8], [11, 0], [11, 1], [11, 2], [11, 3], | |
[11, 4], [11, 5], [11, 6], [11, 7], [11, 8] | |
] | |
expect(image.all_array_indexes).to eql(result) | |
end | |
end | |
end | |
describe '#manhattan cells' do | |
it 'selects and returns only cells within n manhattan distance' do | |
image = Image.new([ | |
[0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0], | |
[0, 0, 1, 0, 0], | |
[0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0] | |
]) | |
cells = [ | |
[0, 2], [1, 1], [1, 2], [1, 3], | |
[2, 0], [2, 1], [2, 2], [2, 3], | |
[2, 4], [3, 1], [3, 2], [3, 3], | |
[4, 2] | |
] | |
expect(image.manhattan_cells(2,2,2)).to eql(cells) | |
end | |
context 'when pixel is in a corner' do | |
it 'return only cells in the dimensions of the array' do | |
image = Image.new([ | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0, 0, 0, 0, 1], | |
]) | |
cells = [ | |
[9,8],[10,7],[10,8],[11,6],[11,7],[11,8] | |
] | |
expect(image.manhattan_cells(11,8,2)).to eql(cells) | |
end | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great work! congrats!