Predict the output
What does this print?
w, limit = [1, 2, 3, 4], 8
count = 0
for i in range(len(w)):
for j in range(i + 1, len(w)):
for k in range(j + 1, len(w)):
if w[i] + w[j] + w[k] < limit:
count += 1
print(count)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 2
Right: 1+2+3 = 6 and 1+2+4 = 7 are under 8; 1+3+4 and 2+3+4 are not.
The brute force agrees with the two-pointer count; checking against it on small inputs is how this page's solution is tested.
Why not the others?
- B 3. 1 + 3 + 4 is exactly 8, which is not strictly less.
- C 4. Only two of the four triples fit.