Created
November 10, 2015 04:19
-
-
Save umasenthil/2d5004f6c5ae87fcbdf9 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 | |
def initialize(darray) | |
@arr = darray | |
end | |
def output_image | |
str = [] | |
ones_indices = [] | |
@height = @arr.length | |
@arr.each_index do |i| | |
@width = @arr[i].length | |
@arr.each_index do |j| | |
if @arr[i][j] == 1 | |
temp = [i,j] | |
ones_indices.push(temp) | |
end | |
end | |
end | |
ones_indices.each_index do |n| | |
temp = ones_indices[n] | |
#puts "i of the location where there is 1 #{temp[0]} #{temp[1]}" | |
i = temp[0] | |
j = temp[1] | |
#change left position to 1 | |
if (j != 0) | |
#puts "change left position to 1" | |
@arr[i][j-1] = 1 | |
end | |
#change top position to 1 | |
if (i != 0) | |
#puts "change top position to 1" | |
@arr[i-1][j] = 1 | |
end | |
#change right position to 1 | |
if (i != @width-1) | |
#puts "change right position to 1" | |
@arr[i+1][j] = 1 | |
end | |
#change bottom position to 1 | |
if (j != @height-1) | |
# puts "change bottom position to 1" | |
@arr[i][j+1] = 1 | |
end | |
end | |
@arr.each_index do |i| | |
str[i] = @arr[i].join('').to_s | |
puts str[i] | |
end | |
return | |
end | |
end | |
image = Image.new([ | |
[0, 0, 0, 0], | |
[0, 1, 0, 0], | |
[0, 0, 0, 1], | |
[0, 0, 0, 0] | |
]) | |
image.output_image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment