Last active
July 29, 2019 18:06
-
-
Save jasonknight/11c5de930a9777611ef9d961d449d106 to your computer and use it in GitHub Desktop.
Trilogy Challenge: Blur Image
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 | |
def initialize(data) | |
@data = data | |
end | |
def get_image() | |
return @data.collect {|row| row.join("") }.join("\n") | |
end | |
def blur(dist=1) | |
new_data = @data.map do |r| | |
r.map do |c| | |
0 | |
end | |
end | |
dist = dist + 1 | |
(new_data.length).times do |i| | |
(new_data[i].length).times do |j| | |
if @data[i][j] == 1 then | |
(dist).times do |ii| | |
new_data[i-ii][j] = 1 if i - ii >= 0 | |
end | |
(dist).times do |ii| | |
new_data[i+ii][j] = 1 if i + ii < new_data.length | |
end | |
(dist).times do |jj| | |
new_data[i][j+jj] = 1 if j + jj < new_data[i].length | |
end | |
(dist).times do |jj| | |
new_data[i][j-jj] = 1 if j - jj >= 0 | |
end | |
# handle diagonals | |
# diagonals are 1/2 the distance | |
(dist).times do |d| | |
n = (d / 2).to_i | |
new_data[i+n][j+n] = 1 if (n < new_data.length and n < new_data[i].length) | |
new_data[i-n][j-n] = 1 if n >= 0 | |
new_data[i-n][j+n] = 1 if n >= 0 and (n < new_data.length and n < new_data[i].length) | |
new_data[i+n][j-n] = 1 if n >= 0 and (n < new_data.length and n < new_data[i].length) | |
end | |
end | |
end | |
end | |
@data = new_data | |
return @data | |
end | |
end | |
tests = [ | |
{ | |
:id => "EmptyData", | |
:data => [], | |
:expect => "" | |
}, | |
{ | |
:id => "SingleRow", | |
:data => [[0,0,0,0]], | |
:expect => "0000" | |
}, | |
{ | |
:id => "SingleRow", | |
:data => [[0,0,0,0],[0,1,0,0],[0,0,0,1],[0,0,0,0]], | |
:expect => "0000\n0100\n0001\n0000" | |
}, | |
] | |
tests.each do |test| | |
i = Image.new(test[:data]) | |
if i.get_image != test[:expect] then | |
puts "#{test[:id]} FAILED" | |
else | |
puts "PASSED" | |
end | |
end | |
puts "----------------" | |
i = Image.new(tests.last[:data]) | |
puts i.get_image | |
blur_tests = [ | |
{ | |
:id => "EmptyData", | |
:data => [], | |
:expect => [], | |
:arg => 1, | |
}, | |
{ | |
:id => "SingleRow", | |
:data => [[0,1,0,0]], | |
:expect => [[1,1,1,0]], | |
:arg => 1, | |
}, | |
{ | |
:id => "OnePixelTransform", | |
:data => [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,1,0,0],[0,0,0,0],[0,0,0,0]], | |
:expect => [[0,0,0,0],[0,0,0,0],[0,1,0,0],[1,1,1,0],[0,1,0,0],[0,0,0,0]], | |
:arg => 1, | |
}, | |
{ | |
:id => "TwoPixelTransform", | |
:data => [[0,0,0,0],[0,0,1,0],[0,0,0,0],[0,1,0,0],[0,0,0,0],[0,0,0,0]], | |
:expect => [[0,0,1,0],[0,1,1,1],[0,1,1,0],[1,1,1,0],[0,1,0,0],[0,0,0,0]], | |
:arg => 1, | |
}, | |
{ | |
:id => "EdgePixelTransform", | |
:data => [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,0,0,0],[0,0,0,0]], | |
:expect => [[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,0,0,0],[1,1,0,0],[1,0,0,0]], | |
:arg => 1, | |
}, | |
{ | |
:id => "ManhattanDistance2", | |
:data => [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,1,0,0],[0,0,0,0],[0,0,0,0]], | |
:expect => [[0,0,0,0],[0,1,0,0],[1,1,1,0],[1,1,1,1],[1,1,1,0],[0,1,0,0]], | |
:arg => 2, | |
}, | |
] | |
blur_tests.each do |test| | |
i = Image.new(test[:data]) | |
if i.blur(test[:arg]) != test[:expect] then | |
puts "#{test[:id]} FAILED " + i.get_image | |
else | |
puts "PASSED" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment