BEST ADVICE OF WEEK: When solving a problem. look at the question and close your computer. Think about exactly how you would solve it given the properties you have been given. Once you have a viable solution given the properties, then open the computer back up and code. Do not let syntax hold you back between what you want to do.
What happened...I was trying to create a NEW array that would put in the array itself and then afterwards it would put in the value min_size - self.length times. This was ridiculous because I didn't need to make a new array, all I needed to do was shovel << value into the existing array. The dev then proceeded to teach me about shallow vs. deep clones which led to me solving the pad method. From now on, I am committed to not being afraid of the syntax itself as it only confuses you when you are solving a problem and just focusing on the logic and the properties that guide it to the most efficient solution. From there, I can look at the syntax and how this will be coded.
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) return self.clone.pad!(min_size, value) end end
No comments:
Post a Comment