Computers and Technology, 23.05.2020 17:00 kiki197701
I got an Three Sum programming problem (Python), and able to come up a quick solution. However, after some more testing i found there seems to be a subtle bug in a test case.
Please help if you can shed some light on it.
The problem is - given an array of integer numbers, try to find the pairs of three numbers that can sum up to '0'. Examples:
array = [1, 0, -1, 2, 3, -2]
answer = [[1, 0, -1], [2, 0, -2]] # those triples add up to '0'
However, if the array is [12, 3, 1, 2, -6, 5, 0, -1, -8, 6]
answer = [[-8, 2, 6], [-8, 3, 5], [-6, 1, 5], [-1, 0, 1]] # missing this: [-6, 0, 6] ?!
code:
```
A. sort()
print(A)
result = []
r = len(A) -1
for x in range(len(A) - 2):
left = x + 1 # for each value of x, left starts one greater
# and increments from there.
while (left < r): # left < right
# sums == all three
sums = A[x] + A[left] + A[r]
if (sums < 0): left += 1
if (sums > 0): r -= 1
if not sums: # 0 is False ---> Not False == True
result. append([A[x],A[left],A[r]])
left += 1 # increment left -when find a combination
print(result)
Answers: 3
Computers and Technology, 22.06.2019 15:30
What is a costume plot? why is it important to a film or theater production?
Answers: 2
Computers and Technology, 23.06.2019 19:30
Of the following pieces of information in a document, for which would you most likely insert a mail merge field?
Answers: 3
Computers and Technology, 24.06.2019 00:00
For the following example of making a peanut butter and jelly sandwich, identify which are inputs, processes, or outputs: bread scooping and spreading peanut butter plate scooping and spreading jelly finished sandwich putting two pieces of covered bread together dirty plate crumbs
Answers: 2
Computers and Technology, 24.06.2019 10:00
Which two technologies support the building of single-page applications?
Answers: 2
I got an Three Sum programming problem (Python), and able to come up a quick solution. However, afte...
English, 11.12.2021 07:40
Business, 11.12.2021 07:50
Computers and Technology, 11.12.2021 07:50
Business, 11.12.2021 07:50
Chemistry, 11.12.2021 07:50
Mathematics, 11.12.2021 07:50
History, 11.12.2021 07:50
Biology, 11.12.2021 07:50
Mathematics, 11.12.2021 07:50
Mathematics, 11.12.2021 07:50