Skip to content

Instantly share code, notes, and snippets.

@NoMan2000
Created February 25, 2019 22:13
Show Gist options
  • Save NoMan2000/3ee892c4598cadfbf5e50945bb5ec86a to your computer and use it in GitHub Desktop.
Save NoMan2000/3ee892c4598cadfbf5e50945bb5ec86a to your computer and use it in GitHub Desktop.

Revisions

  1. NoMan2000 created this gist Feb 25, 2019.
    89 changes: 89 additions & 0 deletions blur.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,89 @@
    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