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
What additive keeps engines clean by preventing contaminates and deposits from collecting on surfaces? a. Friction modifiers b.
White raven [17]

Answer is: Dispersants


Explanation:

Dispersants keep contaminants (e.g. soot) suspended in the oil to prevent them from coagulating. Anti-foam agents inhibit the production of air bubbles and foam in the oil which can cause a loss of lubrication, pitting, and corrosion where entrained air and combustion gases contact metal surfaces.

So that's why the answer is: Dispersants

7 0
4 years ago
Read 2 more answers
You plug a new scanner into a USB port on your Windows system. When you first turn on the scanner, what should you expect to see
Paul [167]

Answer:

A window notifying that: Found New Hardware Wizard

Explanation:

As soon as any device is plugged into the USB port, the default settings will display a window onto the screen which will notify about the addition of a new hardware to the system. This window can further be used to know the properties of the hardware or for configuration settings.

This process is same like the software part, that whenever a new application is installed onto the system a pop up notification alerts about its presence.

So when the scanner will be attached through USB port a window will be appeared notifying about the discovery of new hardware.

3 0
3 years ago
____________________deals with syntax and semantics ofinformation exchange.
Goshia [24]

Answer:

The answer is (a).Presentation Layer

Explanation:

This layer is located at the 6th level of the OSI model, responsible for delivering and formatting the information to the upper layer for further processing. This service is needed because different types of computer uses different data representation. The presentation layer handles issues related to data presentation and transport, including encryption,translation and compression.

3 0
3 years ago
5. What is a domain name used for?​
Mnenie [13.5K]

<u>Answer:</u>

Domain names serve to identify Internet resources, such as computers, networks, and services, with a text-based label that is easier to memorize than the numerical addresses used in the Internet protocols. A domain name may represent entire collections of such resources or individual instances.

<u />

<u>Explanation:</u>

*Hope this helps*

7 0
3 years ago
Your customer, Maggie, wants to buy a gaming PC, and she specifically mentions that she wants an Intel CPU and intends to overcl
Ksenya-84 [330]

Answer:

Intel produces a series of unlocked CPU's that can be overclocked. These CPUs are from the "K" or "X" series. Example: Intel Core i9-9900K, Intel Core i9-10940X.

These are the few things that are to be kept in mind while overclocking:

-Motherboard: Motherboard should support overclocking. Example: Intel Z series, most AMD motherboards.

-Cooler: Boosting the clock speed increases the temperature. The cooler has to be upgraded to keep the temperatures low. Example: Water-cooled. Also, the heat sink has to be checked if it's working properly.

-Be ready to test your system in BIOS. Make sure the temperature, voltage, memory speed is stable for the set clock speed.

Explanation:

8 0
4 years ago
Other questions:
  • If no other row matches, the router will select the ________ row as its best match. First Last either First or Last, depending o
    12·1 answer
  • Janet manages the security of the database servers at the mortgage company where she works. The servers are Windows Server 2016;
    15·1 answer
  • Briefly define "address space" and discuss its major tradeoffs (advantages/costs). What problem is it addressing?
    10·1 answer
  • You might have trouble interpreting a message if:
    15·1 answer
  • What does aperture control? A)amount of light the image sensor captures when taking a photo. B)how sensitive the camera is to in
    10·2 answers
  • In the case below, the original source material is given along with a sample of student work. Determine the type of plagiarism
    15·1 answer
  • Which of the following is another type of brake system used in trucks
    5·1 answer
  • Similarities between inline css and internal css​
    6·1 answer
  • Which option correctly identifies if the researcher’s approach was the best choice in the following scenario and explains why?
    10·2 answers
  • Explain the term creating in word processing​
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!