Skip to main content
Code Review

Return to Question

clarified example
Source Link

I've been working on method blur(n), which outputs an array that has been transformed n times, such that n=3 changes

returnsinto

I've been working on method blur(n), which outputs an array that has been transformed n times, such that

returns

I've been working on method blur(n), which outputs an array that has been transformed n times, such that n=3 changes

into

added 1 character in body; edited tags; edited title
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 479

call ruby method iteratively within a custom class Expanding pixels in an image

Right now my solution to blur(n) works, by first creating a new Image object, transforming and then transforming additional instances of Image an additional (n-1) times. I've been getting errors if I try to apply blur without instantiating a new Image and transforming it--the — the transform method is not available to class Array. How could I go about applying transform iteratively without having to instantiate Image inside the class?

call ruby method iteratively within a custom class

Right now my solution to blur(n) works, by first creating a new Image object, transforming and then transforming additional instances of Image an additional (n-1) times. I've been getting errors if I try to apply blur without instantiating a new Image and transforming it--the transform method is not available to class Array. How could I go about applying transform iteratively without having to instantiate Image inside the class?

Expanding pixels in an image

Right now my solution to blur(n) works, by first creating a new Image object, transforming and then transforming additional instances of Image an additional (n-1) times. I've been getting errors if I try to apply blur without instantiating a new Image and transforming it — the transform method is not available to class Array. How could I go about applying transform iteratively without having to instantiate Image inside the class?

Source Link

call ruby method iteratively within a custom class

Custom class Image takes a 2-D array of 0's and 1's upon initialization. Method transform returns a modified array by modifying 0's adjacent to a 1, such that

[[0,0,0],
 [0,1,0],
 [0,0,0]]

returns

[[0,1,0],
 [1,1,1],
 [0,1,0]]

I've been working on method blur(n), which outputs an array that has been transformed n times, such that

[[0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,1,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0]]

returns

[[0, 0, 0, 0, 0, 0, 0, 0, 0]
 [0, 0, 0, 0, 1, 0, 0, 0, 0]
 [0, 0, 0, 1, 1, 1, 0, 0, 0]
 [0, 0, 1, 1, 1, 1, 1, 0, 0]
 [0, 1, 1, 1, 1, 1, 1, 1, 0]
 [0, 0, 1, 1, 1, 1, 1, 0, 0]
 [0, 0, 0, 1, 1, 1, 0, 0, 0]
 [0, 0, 0, 0, 1, 0, 0, 0, 0]
 [0, 0, 0, 0, 0, 0, 0, 0, 0]
 [0, 0, 0, 0, 0, 0, 0, 0, 0]
 [0, 0, 0, 0, 0, 0, 0, 0, 0]
 [0, 0, 0, 0, 0, 0, 0, 0, 0]]

Right now my solution to blur(n) works, by first creating a new Image object, transforming and then transforming additional instances of Image an additional (n-1) times. I've been getting errors if I try to apply blur without instantiating a new Image and transforming it--the transform method is not available to class Array. How could I go about applying transform iteratively without having to instantiate Image inside the class?

class Image
 attr_accessor :arr
 
 def initialize(arr)
 @arr = arr
 end
 
 def transform 
 cloned = self.arr.map(&:clone)
 #scan original array for 1; map crosses into clone if found
 self.arr.each.with_index do |row, row_index|
 row.each.with_index do |cell, col|
 if cell == 1
 cloned[row_index][col+1] = 1 unless col+1 >= row.length #copy right
 cloned[row_index+1][col] = 1 unless row_index+1 >= cloned.length # copy down
 cloned[row_index][col-1] = 1 unless col.zero? # copy left
 cloned[row_index-1][col] = 1 unless row_index.zero? #copy up
 end
 end
 end
 cloned
 end
 def blur(n) 
 blurred = Image.new(self).arr.transform
 (n-1).times do 
 blurred = Image.new(blurred).transform
 end
 blurred
 end
 
end
 
image = Image.new([[0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,1,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0],
 [0,0,0,0,0,0,0,0,0]])
 
image.blur(3)
 
#returns...
# [[0, 0, 0, 0, 0, 0, 0, 0, 0], 
# [0, 0, 0, 0, 1, 0, 0, 0, 0], 
# [0, 0, 0, 1, 1, 1, 0, 0, 0], 
# [0, 0, 1, 1, 1, 1, 1, 0, 0], 
# [0, 1, 1, 1, 1, 1, 1, 1, 0], 
# [0, 0, 1, 1, 1, 1, 1, 0, 0], 
# [0, 0, 0, 1, 1, 1, 0, 0, 0], 
# [0, 0, 0, 0, 1, 0, 0, 0, 0], 
# [0, 0, 0, 0, 0, 0, 0, 0, 0], 
# [0, 0, 0, 0, 0, 0, 0, 0, 0], 
# [0, 0, 0, 0, 0, 0, 0, 0, 0], 
# [0, 0, 0, 0, 0, 0, 0, 0, 0]]
lang-rb

AltStyle によって変換されたページ (->オリジナル) /