Created
February 25, 2019 22:13
-
-
Save NoMan2000/3ee892c4598cadfbf5e50945bb5ec86a to your computer and use it in GitHub Desktop.
Ruby Image Blur exercise
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 'test/unit' | |
# complete for YouTube video. | |
class Image | |
attr_accessor :outer_array, :new_outer_array | |
def initialize(*arr) | |
@outer_array = arr | |
end | |
def output_image(second_dimensional_array) | |
second_dimensional_array.each do |inner_array| | |
inner_array.each do |individual_element| | |
print individual_element | |
end | |
puts "" | |
end | |
end | |
def blur | |
outer_array_max_length = @outer_array.length | |
# 0 indexing. length though starts at 1. Which means it will be length of five, but end at arr[4]. | |
# Unfortunately, requires a deep copy to make this work. | |
@new_outer_array = Marshal.load( Marshal.dump(@outer_array) ) | |
array_checker = [] | |
@outer_array.each_with_index do |inner_array, outer_arr_index| | |
prev_outer_index = outer_arr_index - 1 | |
next_outer_index = outer_arr_index + 1 | |
inner_array.each_with_index do |individual_element, inner_arr_index| | |
inner_array_max_length = inner_array.length | |
prev_inner_index = inner_arr_index - 1 | |
next_inner_index = inner_arr_index + 1 | |
# Store the ability to mutate a future number so it does not run it before reaching that point. | |
if individual_element == 1 | |
if prev_outer_index >= 0 | |
@new_outer_array[prev_outer_index][inner_arr_index] = 1 | |
end | |
if next_outer_index < outer_array_max_length | |
# TODO: You can't run this yet because it will potentially mutate further down the line. | |
# array_checker.push([next_outer_index, inner_arr_index]) | |
@new_outer_array[next_outer_index][inner_arr_index] = 1 | |
end | |
if prev_inner_index >= 0 | |
@new_outer_array[outer_arr_index][prev_inner_index] = 1 | |
end | |
if next_inner_index < inner_array_max_length | |
# TODO: You can't run this yet because it will potentially mutate further down the line. | |
# array_checker.push([outer_arr_index, next_inner_index]) | |
@new_outer_array[outer_arr_index][next_inner_index] = 1 | |
end | |
end | |
if array_checker.index([outer_arr_index, inner_arr_index]) != nil | |
@new_outer_array[outer_arr_index][inner_arr_index] = 1 | |
end | |
end | |
end | |
end | |
end | |
# Red, green, refactor. | |
class BLUR_TEST < Test::Unit::TestCase | |
def test_blur_will_change_values_to_ones_in_adjacent_places | |
test_arr = [ | |
[0, 0, 1, 0], | |
[0, 0, 0, 0], | |
[0, 1, 0, 1] | |
] | |
image = Image.new( | |
test_arr[0], | |
test_arr[1], | |
test_arr[2] | |
) | |
assert(image.outer_array == test_arr, 'test array and image array do not match') | |
blurred_array_expectation = [ | |
[0, 1, 1, 1], | |
[0, 1, 1, 1], | |
[1, 1, 1, 1] | |
] | |
image.blur | |
blurred_array_actual = image.new_outer_array | |
assert(blurred_array_expectation == blurred_array_actual, 'blurring failed') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment