Skip to content

Instantly share code, notes, and snippets.

@syntaxi
Last active July 9, 2018 03:02
Show Gist options
  • Save syntaxi/e4cb007021cbcb12a72bc0c24dcf5bb1 to your computer and use it in GitHub Desktop.
Save syntaxi/e4cb007021cbcb12a72bc0c24dcf5bb1 to your computer and use it in GitHub Desktop.
Tutor Programming Question

Task

Read a target number, and a list of integers from standard input. Find all pairs of numbers in the list which add exactly to the target number Print out a formatted list of all the pairs

Assumptions

I am assuming that a number cannot be used twice. Ie, if the target is 10 and the list [4,6,5] then the only valid solution is [4,6] I am assuming that order does not matter in a sum. Ie, the pair [4,6] and [6,4] are the same. Lastly, I'm assuming no duplicate numbers. Ie, having [4,4,5,6] as the input is not valid.

I shall be assuming that the first value returned from input() will be the target number. The second value read shall be the length of the list (n), and then the next n reads will be the values in the list Finally any subsequent reads will return None I shall also be assuming that all inputs will be valid integers.

Any concepts the student didn't understand, such as string concatenation would be explained

Solution

Firstly, we should get all of our values from the user

targetNum = int(input()) # The target number

numValues = int(input()) # The number of values to check
values = []
for i in range(numValues): # We will loop that many times, reading and adding the value each time
	values.append(int(input()))

Now we have all of our values, let's find the pairs.

The simplest method we could use is to take the first value, and add it to all the other values. Then repeat this for the second value, and so on. That would look something like this.

pairs = []
for first in values:
	for second in values:
		if first + second == targetNum:
			pairs.append([first, second])

There are two problems with this. We can see those problems if we imagine our target is 10 and our inputs are 4, 5 & 6. We will end up printing out [[4, 6], [5, 5], [6, 4]]. Right away we have both [4, 6] and [6, 4] when we should only have one and we can see that we have used a value twice, with [5, 5].

Let's work out how to fix the first issue, we want to avoid having both 4 + 6 and 6 + 4. Another way of wording this is that once we've checked the values against all the other ones, we want to remove it. With our values of 4, 5 & 6 again we want to check 4 against all the other values. Then if we removed the 4, we would be left with values being 5 & 6. We check the 5 against the whole list, and remove it and we have just 6 left. When we go to check the 6, there is no 4 anymore. This means that we can't get 4 + 6 and 6 + 4

That would look something like this

pairs = []
for first in values:
	for second in values:
		if first + second == targetNum:
			pairs.append([first, second])
	values.remove(first)

Okay, so we've fixed the first problem, but we're still getting the same value added to itself. With a target of 10 and inputs of 4, 5 & 6 we get [[4, 6], [5, 5]] and we don't want the [5, 5].

Well that's actually rather easy. We're already removing the number, so can't we just remove it before we do the check? Let's think about how that would work. When values is 4, 5, 6 we remove the 4 from the list, so we check it against 5 & 6. Then we remove the 5 from the list and check it against the 6 Finally we remove the 6, and our list is now empty so we don't check it against anything.

That would look something like this

pairs = []
for first in values:
	values.remove(first)
	for second in values:
		if first + second == targetNum:
			pairs.append([first, second])

So we've finished calculating the pairs properly, but we are not quite done. We still need to print them nicely.

for pair in pairs:
	print(str(pair[0]) + " + " + str(pair[1]) + " = " + str(targetNum))

So finally the entire solution would look like this:

targetNum = int(input())
numValues = int(input())
values = []
for i in range(numValues):
	values.append(int(input()))
  
pairs = []
for first in values:
	values.remove(first)
	for second in values:
		if first + second == targetNum:
			pairs.append([first, second])
      
for pair in pairs:
	print(str(pair[0]) + " + " + str(pair[1]) + " = " + str(targetNum))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment