Created
February 17, 2017 12:48
-
-
Save arcolife/8a23820ef97749db875294d4c7fe795e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/env python3 | |
| from collections import Counter | |
| pairs = [] | |
| def find_pairs(A,x): | |
| V = {k:v for k,v in A.items() if k<=x} | |
| visited = [] | |
| for i in V: | |
| if V[i]==0: | |
| continue | |
| c = x-i | |
| if c in V: | |
| visited.append(i) | |
| if c == i and V[c] > 1: | |
| pairs.append([c,i]) | |
| V[c] -= 2 | |
| else: | |
| pairs.append([c,i]) | |
| V[c] -= 1 | |
| V[A[i]] -= 1 | |
| F = set(V.keys())-set(visited) | |
| for i in F: | |
| V[i] = 0 | |
| Z = {k:v for k,v in V.items() if v==1} | |
| if Z: | |
| find_pairs(Z,x) | |
| else: | |
| return | |
| x = [3,1,5,5,2,8,6] | |
| find_pairs(Counter(x),10) | |
| assert pairs == [[8, 2], [5, 5]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.