Created
April 30, 2020 05:25
-
-
Save kurianbenoy/c0f9053f9b5a7f41f9faf6b84aeb7a35 to your computer and use it in GitHub Desktop.
This file contains 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
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') |
$ 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
Much better. Still a few warts
find_pairs
val
as an argument if you are using 1000000 as a literal in the code ? Use either not both. I suggest changing the hard-coded 1000000 toval
.val
a good name ? Isn'ttotal
better for example ?snake_case
and let the variables have some context in ther naming. So in this case,num_1
,num_2
ornums_1
,nums_2
are better than say evenm1
orm2
.Refer here for different casing approaches: https://medium.com/better-programming/string-case-styles-camel-pascal-snake-and-kebab-case-981407998841
find_pairs_files
int(...)
on a string drops its new-line as well. So no need to dostrip()
- you save some processing time and your code runs faster. Try it.Overall
Right now your code rating is still about 6.5/10 for me - fixing the variable names and removing dead code would take it up to 8/10. Dropping extra white space would make it 9/10 and adding doc-strings and fixing the logic to increase speed will make it 9.5/10 .
For reference here is my solution - https://gist.github.com/pythonhacker/1cf74fb0f1ea3f6f99bc94566651958e
Timing
Here is your code timing (keeping the redundant "s" list) (For timing copy the function
timer_test
from my code and modify it accordingly.After commenting out the "s" code,
Hope the feedback makes you a better programmer. All the best.