Skip to content

Instantly share code, notes, and snippets.

@gusennan
Created April 27, 2020 18:28
Show Gist options
  • Save gusennan/ed529d0ca3af387db222dd967e9eb49d to your computer and use it in GitHub Desktop.
Save gusennan/ed529d0ca3af387db222dd967e9eb49d to your computer and use it in GitHub Desktop.
pmap
import sys
import random
import string
from itertools import combinations, repeat
from multiprocessing import Pool, cpu_count
from typing import List, Iterable, Tuple
def random_word(length: int) -> str:
return ''.join(random.choice(string.ascii_lowercase) for i in range(length))
def hamming_distance(tup: Tuple[str, str]) -> int:
return sum(1 if x == y else 0 for x, y in zip(tup[0][tup[1]], tup[0][tup[2]]))
def pairs(upto: int) -> Iterable[Tuple[int, int]]:
return combinations(range(upto), 2)
def main(args: List[str]) -> int:
num_sequences = 1800
sequences = [random_word(1000) for _ in range(num_sequences)]
args_to_hamming_distance = ((sequences, seq1, seq2) for seq1, seq2 in pairs(num_sequences))
with Pool() as pool:
results = pool.map(hamming_distance, args_to_hamming_distance)
for (seq1, seq2), result in zip(pairs(num_sequences), results):
print(f'{result}, {seq1} <-> {seq2}')
return 0
if __name__ == "__main__":
exit(main(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment