-
-
Save fcheung/d8e27a113e684759a84f to your computer and use it in GitHub Desktop.
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
class Image | |
attr_accessor :data | |
def initialize (data) | |
@data = data | |
end | |
def image_blur | |
changes = [] | |
@data.each_with_index do |row, row_index| | |
row.each_with_index do |value, column_index| | |
if value == 1 | |
changes << [row_index, column_index-1] if column_index > 0 | |
changes << [row_index-1, column_index] if row_index > 0 | |
changes << [row_index+1, column_index] if row_index < @data.size-1 | |
changes << [row_index, column_index + 1] if column_index < row.size - 1 | |
end | |
end | |
end | |
changes.each do |(row_index, column_index)| | |
@data[row_index][column_index] = 1 | |
end | |
end | |
def output | |
@data.each do |sub| | |
sub.each do |cell| | |
print cell | |
end | |
puts "\n" | |
end | |
end | |
end | |
image = Image.new([[0,0,0,0,0,0,0], | |
[0,1,1,0,0,0,1], | |
[0,0,0,0,0,0,0] | |
]) | |
image.image_blur | |
image.output | |
test "pixel on left hand side" do | |
image = Image.new([[0,0,0,0,0,0,0], | |
[1,0,0,0,0,0,0], | |
[0,0,0,0,0,0,0] | |
]) | |
image.image_blur | |
assert_equal( | |
[[1,0,0,0,0,0,0], | |
[1,1,0,0,0,0,0], | |
[1,0,0,0,0,0,0]], | |
image.data | |
) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment