a/A practice challenge problem: luckysevens?
As I’m still figuring out what coding bootcamp is best for me, I’m preparing for admissions to all three of my top choices. App Academy is one of them, and right now I’m working through their admissions prep. The following is exactly how I worked through the first problem of their practice code challenge. This is copy/pasted directly from my codeanywhere.com IDE--I notate the shit out of my code because it forces me to think through the problem and understand my solution well enough that I can teach the methods learned to another newbie. Ruby docs are my world right now!
**Warning: Solution included! Hopefully you’ve found this page after struggling with the problem on your own first.
#"Write a function lucky_sevens?(numbers), which takes in an array of integers and returns true if any three consecutive elements sum to 7.
#lucky_sevens?([2,1,5,1,0]) == true # => 1 + 5 + 1 == 7
#lucky_sevens?([0,-2,1,8]) == true # => -2 + 1 + 8 == 7
#lucky_sevens?([7,7,7,7]) == false
#lucky_sevens?([3,4,3,4]) == false
#Make sure your code correctly checks for edge cases (i.e. the first #and last elements of the array)."
#Step 1: Define lucky_sevens?(numbers)
#Step 2: Iterate over every element in groups of 3.
#Step 3: During each iteration, sum the group and push sum to an array.
#Step 4: If sums array elements include 7, return true; else, return false.
def lucky_sevens?(numbers)
sums = [ ]
numbers.each_cons(3) {|e| sums.push(e.reduce(:+))} #a/b
if sums.include?(7) #c
puts true
else
puts false
end
end
#test:
lucky_sevens?([1, 2, 4, 6]) # expected true
lucky_sevens?([8, 9, -10, 11]) # expected true
lucky_sevens?([3, 5, 6, 1]) # expected false
lucky_sevens?([2,1,5,1,0]) # expected true
lucky_sevens?([0,-2,1,8]) # expected true
lucky_sevens?([7,7,7,7]) # expected false
lucky_sevens?([3,4,3,4]) # expected false
#a: My initial thought process had me determining within each loop whether there's a sum of 7. Instead, this line iterates over each element of the input array in groups of 3, then the 3 element chunks are reduced within the iteration to a sum. That sum is then pushed to the empty array declared within the method. I wrote this solution hours before getting it to work. If I've learned anything about myself in the last 28 years, it's that I prefer to learn through suffering than learn from someone else's answer. So, I spent hours stuck in a cycle of incorrect output before I finally searched Google for the hints to a solution.
#b: The solutions I found on a couple of different forums were unsatisfactory in my opinion. They were not as simple or as easy to read and understand as the one I was trying to write. While reading through the comments of one forum (facebash) I had an aha moment and realized my problem was stupidly simple--I was using '<<' to populate the array with the sums instead of '.push'. These essentially do the exact same job, with one important distinction that I wasn't aware of: '<<' only allows the population of a single element at a time, while '.push' allows multiple elements to be pushed in one line of code! After fixing this, it finally worked.
#c: This if statement checks the resulting array of sums for the presence of the number 7, and outputs 'true' if detected; otherwise the output is false.
Have a refactor suggestion or a different solution to share? Help other new programmers and answer below!