A problem statement from Arden Dertat's "programming questions" series:
Given an integer array, output all pairs that sum up to a specific value k.
The first method need not be always the best method, but I try to get the result right without caring about the algorithm or efficiency.
This method revolves around the use of a function that returns the sum of two array elements. By doing a N*(N-1) loop, each element is checked with every other element which is greater than or equal than that. Finally a tuple is appended to a list after checking for repetitions.
An efficient (and my favorite method) to do the same in a few lines is to use list comprehension. The algorithm works by finding the absolute difference, x, of k from all elements in the array and then looking for a matching y in the array so that their sum is k. To return unique pairs from the list, I cheat by using the set() function. Pretty straight forward.