Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save carolineartz/0509443883da973d9816 to your computer and use it in GitHub Desktop.
Save carolineartz/0509443883da973d9816 to your computer and use it in GitHub Desktop.
Implement Array#pad and Array#pad!. Each method accepts a minimum size (non-negative integer) and an optional pad value as arguments. If the array's length is less than the minimum size, Array#pad should return a new array padded with the pad value up to the minimum size. If the minimum size is less than or equal to the length of the array, it s…
# PSEUDOCODE
# INPUT: array object takes a non-negative integer minimum array size,
# optional value to pad which defaults to nil
# OUPUT: array object (#pad! returnning the same object while #pad
# will return a copy) which is padded conditional on its existing
# attributes and the passed parameters
# STEPS: for #pad! [#pad will do the same, but make a copy prior to executing
# anything with the potential to modify it] if the array is smaller than the
# minimum size find the difference and push the value, or nil if parameter
# omitted, to the end of the array and return the array
# INITIAL CODE:
class Array
def pad!(min_size, value = nil)
if self.length < min_size
(min_size - self.length).times do
self << value
end
end
return self
end
def pad(min_size, value = nil)
new_array = self.dup
if new_array.length < min_size
(min_size - new_array.length).times do
new_array << value
end
end
return new_array
end
end
# REFACTORED CODE:
class Array
def pad!(min_size, value = nil)
while self.length < min_size
self << value
end
return self
end
def pad(min_size, value = nil)
padded = self.dup
while padded.length < min_size
padded << value
end
return padded
end
end
# REVIEW/REFLECT I worked on this problem during my first (not moderated)
# pairing session. Tackling pair coding was tough and this problem wasn't the
# easiest of ones we have seen thus far. Being the first challenge or required
# pre-phase 0 exercise that presented us with a class, we were thrown off from
# the start trying to agree on what to put for psuedocode INPUT. Further, I
# think we were nervous or overthinking the problem because we couldn't quite
# agree on what calculation needed to be done to test the length of the array
# against the minimum size and determine whether further action was needed. It
# was a learning experience in more ways than one. Ultimately, I came up with
# a working solution (INITIAL CODE) shortly after breaking with my pair. We
# reconviened the following day and worked through what we had come up with.
# While thinking about approaches for refactoring and checking which avenues my
# fellow boots took on the way to a solution, I wondered why I even called for
# if/else testing the length. A while statement would do this by definition and
# could easily replace and simplify my solution, if only a little. I look
# forward to looking into this problem more for the next challenge.
@tihuan
Copy link

tihuan commented Jan 13, 2014

Hello! I'm also a DBC student :-) As part of a challenge I'm asked to add a comment to a fellow student's code. So here's my 2 cents:

Instead of copy/paste codes from method "pad!" to method "pad", I just learned to do the following instead:

def pad(min_size, value = nil)
self.dup.pad!(min_size, value)
end

Hope this helps!

@carolineartz
Copy link
Author

yes! thank you! I learned about #dup also. you should check out #clone as well -- here it is in my second pad array challenge :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment