3 ms·
I thought I spotted a bug on the very first day, part 2. def day1_2(nums): "Find 3 distinct numbers that sum to 2020, and return their product." retu
by dmurray 6y ago
I thought I spotted a bug on the very first day, part 2.
def day1_2(nums):
"Find 3 distinct numbers that sum to 2020, and return their product."
return first(x * y * z
for x, y in combinations(nums, 2)
for z in nums & {2020 - x - y}
if x != z != y)
The last line (if x != z != y) returns true when x and y are equal and z is different.
But x and y are already constrained to be different since nums is a set and itertools combinations picks distinct elements from the iterable. If the test had been x != y != z it would have been a bug.
- norvig 6y agoGood point, dmurray. It was a subtle point here, and probably I should have commented on it.
- deleted 6y ago[deleted]