Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.0 kB
3
Indexable
Never
"""
Computer Systems Architecture - Lab 3

Threadpool assignment:
    Use a pool of threads to search for a DNA sequence in a list of DNA samples
"""
import random
from concurrent.futures import ThreadPoolExecutor as ThreadPool

random.seed(0)

# generate many DNA samples, need to be large, otherwise there's no point of a concurrent program
dna_samples = [''.join(random.choices('ATGC', k=10000)) for _ in range(100)]

# Afișăm eșantioanele generate
# for i, sample in enumerate(dna_samples):
#     print(f"Sample {i}: {sample[:100]}...")  # Afișăm doar primele 100 de caractere pentru a evita o ieșire prea lungă

# Selectăm un subșir aleator dintr-unul dintre eșantioanele generate
selected_sample_index = random.randint(0, len(dna_samples) - 1)
start_index = random.randint(0, len(dna_samples[selected_sample_index]) - 11)  # Se asigură că subșirul are cel puțin 11 caractere
SEARCH_SEQUENCE = dna_samples[selected_sample_index][start_index:start_index + 11]

def search_dna_sequence(sequence, sample):
    """
    TODO 4: Search a DNA sample in a DNA sequence
    :return: True if the sequence was found, False otherwise
    """
    if sequence.find(sample) != -1:
        return True
    else:
        return False


def thread_job(sample_index):
    """
    Each thread searches the sequence in a given sample
    """
    if search_dna_sequence(SEARCH_SEQUENCE, dna_samples[sample_index]):
        print("FOUND!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
        return "DNA sequence found in sample {}".format(sample_index)
    return "DNA sequence not found in sample {}".format(sample_index)


if __name__ == "__main__":

    thread_pool = ThreadPool(max_workers= 30)

    futures = []

    with thread_pool:
        # TODO5: execute thread job using submit or map
        result = thread_pool.map(thread_job, range(len(dna_samples)))
        # TODO6: print results
        for r in result:
            print(r)
Leave a Comment