Skip to content

Instantly share code, notes, and snippets.

@kurianbenoy
Created April 30, 2020 05:25
Show Gist options
  • Save kurianbenoy/c0f9053f9b5a7f41f9faf6b84aeb7a35 to your computer and use it in GitHub Desktop.
Save kurianbenoy/c0f9053f9b5a7f41f9faf6b84aeb7a35 to your computer and use it in GitHub Desktop.
import random
def find_pairs(M1, M2, val):
s =list()
for i in range(0,1000000):
s.append(M1[i])
diff = {1000000 -n:n for n in M1}
pairs = []
# diff = []
# for i in range(0,1000000):
# diff.append(1000000 - M1[i])
# print(diff)
# print(M2[i])
# pairs = []
# for i in range(0,1000000):
# if(diff[i]==M2[i]):
# pairs.append((diff[i], M1[i]))
for n in M2:
if n in diff:
pairs.append((diff[n], n))
# print(pairs)
return pairs
def generate_files(filename,size):
random.seed()
with open(filename, 'w') as f:
for _ in range(size):
f.write(f"{random.randint(10,1000000)}\n")
def find_pairs_files(fname1, fname2):
M1 = []
M2 = []
with open(fname1) as f:
for line in f:
line = line.strip()
M1.append(int(line))
with open(fname2) as f:
for line in f:
line = line.strip()
M2.append(int(line))
pairs = find_pairs(M1,M2,1000000)
print(f'Found {len(pairs)} pairs')
print(len(M1), len(M2))
if __name__ == "__main__":
find_pairs_files('random1.txt','random2.txt')
@kurianbenoy
Copy link
Author

kurianbenoy commented May 1, 2020

$ python3 hashing.py
Found 632844 pairs
Found 632844 pairs
Found 632844 pairs
Found 632844 pairs
Found 632844 pairs
Found 632844 pairs
Found 632844 pairs
Found 632844 pairs
Found 632844 pairs
Found 632844 pairs
Found 632844 pairs
Found 632844 pairs
TIme per pass => 1.3160592321000877

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment