def ndFindNANA(inString): # the list of strings to look for strings = ['CACA', 'GAGA', 'TATA', 'AAAA'] # create an empty list that will store our threads threads = [ ] # create an object that can store a nondeterministic solution # computed by other threads nonDetSolution = utils.NonDetSolution() # create the threads that will perform the nondeterministic # computation for string in strings: # create a thread that will execute # findString(string, inString, nonDetSolution) when started t = Thread(target=findString, args = (string,inString,nonDetSolution)) # append the newly-created thread to our list of threads threads.append(t) # Perform the nondeterministic computation. By definition, this # means that each thread is started, and we get a return value if # either: (a) any thread reports a positive solution, or (b) all # threads report negative solutions. solution = utils.waitForOnePosOrAllNeg(threads, nonDetSolution) return solution
def ndContainsNANA(inString): # the list of strings to look for strings = ['CACA', 'GAGA', 'TATA', 'AAAA'] # create an empty list that will store our threads threads = [ ] # create an object that can store a nondeterministic solution computed by # other threads ndSoln = utils.NonDetSolution() # create the threads that will perform the nondeterministic computation for s in strings: # create a thread that will execute findString(s, inString, ndSoln) # when started; findString is a helper function defined below t = Thread(target=findString, args = (s, inString, ndSoln)) # append the newly created thread to our list of threads threads.append(t) # Perform the nondeterministic computation. By definition, this means # that each thread is started, and we get a return value if either (a) # any thread reports a positive solution, or (b) all threads report # negative solutions. solution = utils.waitForOnePosOrAllNeg(threads, ndSoln) return solution
def ndFindNANAHelper(text, parentNdSoln, depth=0): if depth == maxDepth: # We are at a leaf of the computation tree. solution = findNANA(text) parentNdSoln.setSolution(solution) else: # We are at an internal node of the computation tree. Split the # given text into two halves, and launch new threads to search # each half separately. threads = [] childNdSoln = utils.NonDetSolution() halfway = int(len(text) / 2) # We will split the input text in half, but we could get # unlucky and find that one of the desired substrings # straddles the point of the split. Therefore, extend both # halves by the maximum length of a desired substring. upperLimit = min(halfway + maxDesiredStringLen, len(text)) lowerLimit = max(halfway - maxDesiredStringLen, 0) text1 = text[:upperLimit] text2 = text[lowerLimit:] t1 = Thread(target=ndFindNANAHelper, args=(text1, childNdSoln, depth + 1)) t2 = Thread(target=ndFindNANAHelper, args=(text2, childNdSoln, depth + 1)) threads.append(t1) threads.append(t2) solution = utils.waitForOnePosOrAllNeg(threads, childNdSoln) parentNdSoln.setSolution(solution)
def hasMultipleOfK (nums): integers = [] integers = map(int, input().split()) k = int(input(":")).append(integers) mkSoln = utils.NonDetSolution() for k in integers: t = Thread(target=findInt, args = (k, nums, mkSoln)) integers.append(t) solution = utils.waitForOnePosOrAllNeg(integers, mkSoln) return solution
def GthenOneT(inString): threads = [ ] ndSoln = utils.NonDetSolution() for i in range(0, len(inString)): if inString[i] == 'G': leftString = inString[:i] leftThread = Thread(target=findT, args = (leftString, ndSoln)) threads.append(leftThread) rightString = inString[i+1:] rightThread = Thread(target=findT, args = (rightString, ndSoln)) threads.append(rightThread) return utils.waitForOnePosOrAllNeg(threads, ndSoln)
def findMultipleOfK(): integers = [] integers = map(int, input().split()) output = [] k = int(input(":")).append(integers) nonDetSolution = utils.NonDetSolution() for k in integers: if integers % k == 0: output.append(integers[k]) t = Thread(target=findInt, args = (integers, nonDetSolution)) integers.append(t) solution = utils.waitForOnePosOrAllNeg(integers, nonDetSolution) return solution
def ndFactor(inNum, K): num = int(inNum) numRt = int(math.sqrt(num)) threads = [] ndSoln = utils.NonDetSolution() numList = numpy.array_split(range(2, numRt + 1), K) for L in numList: t = Thread(target=findFactor, args=(L, num, ndSoln)) threads.append(t) solution = utils.waitForOnePosOrAllNeg(threads, ndSoln) return solution
def main(args): M = int(args.inString) M_prime = int(np.sqrt(M)) search_range = np.array([i + 2 for i in range(M_prime - 2)]) splitted = np.array_split(search_range, args.K) ndSoln = utils.NonDetSolution() threads = [] for split_range in splitted: t = Thread(target=factor_opt, args=(M, split_range, ndSoln)) threads.append(t) res = utils.waitForOnePosOrAllNeg(threads, ndSoln) return res
def ndFactorHelper(M, low, high, nonDetSolution): if high <= low: # There is nothing to search. return elif high - low == 1: # The search interval includes only one possibility (low), so test it. if M % low == 0: # low is a factor of M, so store this solution nonDetSolution.setSolution(str(low)) return else: # Split the search interval in two, and launch new threads to # search those intervals. threads = [] childNdSoln = utils.NonDetSolution() mid = int((high + low) / 2) t1 = Thread(target=ndFactorHelper, args=(M, low, mid, childNdSoln)) t2 = Thread(target=ndFactorHelper, args=(M, mid, high, childNdSoln)) threads.append(t1) threads.append(t2) solution = utils.waitForOnePosOrAllNeg(threads, childNdSoln) nonDetSolution.setSolution(solution)