I have a list of> 10.000 int items. The values of the elements can be very high, up to 10 ^ 27. Now I want to create all pairs of elements and calculate their sum. Then I want to look for different pairs with the same amount.
For instance:
l[0] = 4
l[1] = 3
l[2] = 6
l[3] = 1
...
pairs[10] = [(0,2)]
pairs[7] = [(0,1), (2,3)]
pairs[5] = [(0,3)]
pairs[9] = [(1,2)]
...
Content pairs[7]is what I'm looking for. This gives me two pairs with the same sum of values.
I implemented it as follows - and I wonder if this can be done faster. Currently, 10,000 items require more than 6 hours in a fast car. (As I said, values land therefore keys pairsare ints up to 10 ^ 27.)
l = [4,3,6,1]
pairs = {}
for i in range( len( l ) ):
for j in range(i+1, len( l ) ):
s = l[i] + l[j]
if not s in pairs:
pairs[s] = []
pairs[s].append((i,j))
Edit: I want to add a little background, as requested by Simon Stelling.
The goal is to find formal analogies, for example
lays : laid :: says : said
[ lays, lay, laid, says, said, foo, bar ... ]
analogy(a,b,c,d), True, a : b :: c : d. , , , O ((n ^ 4)/2).
, char -count. , char (a, d) (b, c). , "layssaid" 2 a, "laidsays"
,
- , " char" (
l) pairs , " ", .. count char.
, . O ((n ^ 2)/2), , .