about summary refs log tree commit diff
path: root/users/wpcarro/scratch/facebook/moderate/find-pairs-for-sum.py
blob: 69c2fc431296527edaeed56c447d870a59f88f17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import random

def find_pairs(xs, n):
    """
    Return all pairs of integers in `xs` that sum to `n`.
    """
    seeking = set()
    result = set()
    for x in xs:
        if x in seeking:
            result.add((n - x, x))
        else:
            seeking.add(n - x)
    return result

xs = [random.randint(1, 10) for _ in range(10)]
n = random.randint(1, 10) + random.randint(1, 10)
print("Seeking all pairs in {} for {}...".format(xs, n))
print(find_pairs(xs, n))