1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Karolina [17]
3 years ago
6

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)
Computers and Technology
1 answer:
Darya [45]3 years ago
6 0

Answer:

  r = len(A) -1 # needs to be inside the <em>for x</em> ... loop

Explanation:

After appending a result sum to your output list, your value of r remains at its last value. In the case of {-6, 0, 6}, you miss this because r is pointing to "5" after having just located the triple {-8, 3, 5}.

When "left" is reset for a new value of x, "r" needs to be reset, too.

_____

<em>Comment on the algorithm</em>

You have an interesting algorithm here. When written as a procedure in Mathematica, it executes your example problem in 280 us. A less procedural approach that generates all subsets of length 3, then sums those, then picks the ones that have a sum of zero executes in 120 us. Of course, keeping lists of all subsets and their sums uses a lot more memory than your program does.

You might be interested in
If you need to set up direct deposit, which information from your check would you likely need?
scoray [572]
The bank routing number and checking account number will be needed.
5 0
3 years ago
Pls help me to answer this question ASAP
mariarad [96]
It’s about yourself. Answer the questions on how you’ve been experiencing them in your life
7 0
3 years ago
Which PlayStation was the first to allow connection between it and computer network
Bezzdna [24]

If you're talking about connecting to the internet internet, it would be the PS2. You could buy an adapter for an ethernet cable to allow for online play.

4 0
4 years ago
When should you stop where you are, drop to the
Vedmedyk [2.9K]

Answer:

A . if you burn a stump because if u roll over the fire will have a 90% chance of going out

7 0
3 years ago
Chris has just graduated from high school. He hopes to complete a carpenter's apprenticeship and eventually open his own busines
malfutka [58]
I would want to say a goal.
4 0
3 years ago
Read 2 more answers
Other questions:
  • Describe the difference between gui and cli​
    9·1 answer
  • To extend the bottom border of a hyperlink across the complete width of a navigation list, change the ____ property of each hype
    9·1 answer
  • What is an implicit benefit to Monetary Policy?
    6·1 answer
  • which field in the contact form is used to control the order in which contacts are displayed in the current view
    15·1 answer
  • Which of the following are engine features of 2018 TITAN XD Diesel but not of 2018 TITAN XD Gas?
    13·1 answer
  • 50 pts. please help ! Explain briefly the role, responsibilities, and required background of the production designer of a film.
    12·1 answer
  • A key requirement for the process of testing hypotheses in the scientific method is Group of answer choices experimenter bias. c
    9·1 answer
  • I need help with this question!!
    8·1 answer
  • You are the network administrator for a small company that implements NAT to access the internet. However, you recently acquired
    13·1 answer
  • How do you<br>know which Finance system <br>is the right one for an<br>Institutions<br>​
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!