<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">from threading import Thread
import math
import sys
import time

#Here, our task is to find how many negative integers exist in
#a list of integers. However, we take advantage of threads,
#in order to parallelize the work and finish sooner!
#More specifically, the program, first, generates n thread-workers.
#Then, each thread becomes responsible to search a specific
#part of the list with integers and it returns the number of 
#negative integers that it found. At the end, the program waits
#until all the workers have finished, it collects their partial
#results, and then, it outputs their summation. The number of
#thread-workers that should be generated is given as an argument to
#the program upon its execution.

#Start counting the execution time
start_time = time.time()

#Read number of threads
threads_num = int(sys.argv[1])

#List of integers
integers =[ 3, 5, 8, -12, 45, 678, 34, 9, 5, 78, 
0, 1, -2, 33, 45, 78, 90, 4567, 23, 100,
-56, 4, 78, 96, 245, -4890, 56, 66, 2, 3,
98, -7, -35, 1, 56, 346, -42, -87, 9, 10]
size = len(integers)

#Array with partial counters of the threads
partial_results = [0 for i in range(threads_num)]



class Worker(Thread):

    def __init__(self,myid):

        Thread.__init__(self)
		#Each thread is identified by a unique id that is decided
		#upon the thread's creation.
        self.myid=myid

    def run(self):
	
	#Determine the part of the list that is under the responsibility
	#of this thread (first and last element that the thread should examine).
        #Fix me!
	   
	#Repeat the following check for each element in the thread's partition.
        #Fix me!
		
            #Check if the element is negative
            if integers[i]&lt;0:
	        #If yes, icrease the thread's partial counter
                partial_results[self.myid] = partial_results[self.myid] + 1
		#and, take a nap :)
                time.sleep(0.5)
	

	
###############################################
# I am the parent and I will create n threads #
###############################################

#We create an array of threads.
#For each thread, which we name it "Worker", we pass as an argument
#a unique id.
workers = [Worker(i) for i in range(threads_num)]

#Begin the execution of the threads!
for i in range(threads_num):
    workers[i].start()

#Wait the threads to finish!
for i in range(threads_num):
    workers[i].join()

#Now, that my children finished calculating, I will calculate
#the final result.

#Fix me!

#...And I will print the result!
print ('Number of negative integers= ' + str(final_result))

#...Together with the execution time!
print (str(time.time() - start_time) + ' seconds')
</pre></body></html>