Reputation: 599
I am working through the prep materials for my application to a coding bootcamp. This is a practice problem I am struggling with (using Python):
"Write a function 'lucky_sevens(numbers)', which takes in a list of integers and print True if any three consecutive elements sum to 7. Make sure your code correctly checks for the first and last elements of the array."
I know how to loop through an array one element at a time, but don't know how to "hold" one element while also assessing the second and third elements in relation to the first, as this prompt requires. As you can see from my attempt below, I'm not sure when/where/how to increment the index values to search the whole list.
def lucky_sevens(numbers):
index1 = 0 # For assessing 1st of 3 numbers
index2 = index1 + 1 # For assessing 2nd of 3 numbers
index3 = index2 + 1 # For assessing 3rd of 3 numbers
# If all index values are within the list...
if index1 <= (len(numbers) - 2) and index2 <= (len(numbers) - 1) and index3 <= len(numbers):
# If the values at those indices sum to 7...
if numbers[index1] + numbers[index2] + numbers[index3] == 7:
print True
else:
print False
# I think the increments below may be one of the places I am incorrect
index1 += 1
index2 += 1
index3 += 1
When I run
lucky_sevens([2, 1, 5, 1, 0])
It is printing False, I think because it is only considering elements in the 0th, 1st and 2nd positions (sums to 8, not 7, as required).
It should print True, because elements in the 1st, 2nd and 3rd positions sum to 7. (1 + 5 + 1 = 7).
Can anyone please provide a suggestion? I would be most appreciative.
Upvotes: 3
Views: 2207
Reputation: 1010
What about using recursion?
def lucky_sevens(numbers, index=0):
if index <= len(numbers):
if sum(numbers[index:index + 4]) == 7:
return True
else:
index += 1
return lucky_sevens(numbers[1:], index)
return False
Upvotes: 0
Reputation: 90979
Yes, for your case its only considering the first, second and third elements. This is because you do not have any loops in your function.
In Python loop constructs are for
and while
. So you would need to use either one.
I can give you some hints to the problem , not going to provide you the complete code (since otherwise how would you learn?) -
You need to loop through the indexes from first index (0) to the len(numbers) -2
. An easy function that can help you do this would be enumerate()
, it spits out the index as well as actual element when iterating over it using for
loop (If you are using enumerate , you would need to put a condition to check that index should be less than len(numbers) - 2
).
You should then get the elements from index+1
pos and index+2
position as well, and sum them and check if thats equal to 7
, if so you should return True
.
A common mistake many make is to return False
if the above (2) condition is not met, but actually what you need to do is to return it only when there are no matches at all (at the end of the function) .
Upvotes: 2
Reputation: 755
Try this:
def lucky_sevens(numbers):
for i in range(0, len(numbers)):
if sum(numbers[i:i + 3]) == 7:
print True
return
print False
The reason yours doesn't work is because you're not looping it, you only check the first 3 elements in the list.
Upvotes: 1
Reputation: 1516
You need a loop through the list to evaluate all elements. In your code, you only evaluate the first 3 elements.
Upvotes: 1