Created
October 19, 2017 17:43
-
-
Save Bovojon/accc04715de7dda63967cbc4e0292ab2 to your computer and use it in GitHub Desktop.
Given a stream of process ids associated with process start and finish actions, output the one process id which has not yet finished, or 0 if all processes are accounted for.
This file contains hidden or 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
''' | |
Given a stream of process ids associated with process start and finish actions, | |
output the one process id which has not yet finished, or 0 if all processes are | |
accounted for. | |
''' | |
def finished_processes(ids): | |
''' | |
It would have been too easy to use the count() method in python - the solution | |
would have been in a few lines of code only: | |
###### | |
def finished_processes(ids): | |
count = 0 | |
for idd in ids: | |
if ids.count(idd) == 1: | |
count += 1 | |
print(idd) | |
if count == 0: | |
print 0 | |
###### | |
Hence, I used another manual method. | |
''' | |
for idd in ids: | |
count = 0 | |
for other_idd in ids: | |
if idd == other_idd: | |
count+=1 | |
if count == 2: | |
for i in range(count): | |
ids.remove(other_idd) | |
if len(ids) == 1: | |
print ids[0] | |
else: | |
print 0 | |
if __name__ == '__main__': | |
finished_processes([36,47,58,47,36]) | |
finished_processes([12,23,34,34,12,23]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment