from BinarySearch import binarySearch # Binary search, base case: Empty sequence assert (binarySearch([], 4) == -1) # Binary search, base case: 1-element sequence assert (binarySearch([2], 1) == -1) assert (binarySearch([2], 2) == 0) # Binary search, recursive case: 3-element sequence assert (binarySearch([5, 7, 8], 4) == -1) assert (binarySearch([5, 7, 8], 5) == 0) assert (binarySearch([5, 7, 8], 7) == 1) assert (binarySearch([5, 7, 8], 8) == 2) assert (binarySearch([5, 7, 8], 9) == -1) # Binary search, recursive case: assert (binarySearch([0, 0, 0, 0, 0], 0) == 2)
from BinarySearch import binarySearch def insertionSort(arr): """ Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands. :param arr: :return: arr Complexity O(n^2 """ for i in range(len(arr)): current = arr[i] j = i - 1 while j >= 0 and arr[j] > current: arr[j + 1] = arr[j] j = j - 1 arr[j + 1] = current return arr x_array = [54, 26, 93, 17, 77, 31, 44, 55, 20, 89, 0] print("Insertion Sort Algorithm: " + str(insertionSort(x_array))) y = binarySearch(55, x_array, 0, len(x_array) - 1) print(y)
def test_case_2(self): self.assertEqual(binarySearch([1, 5, 23, 111], 5), 1)
#load the long list of text or makei t try: if os.path.exists("url_text.txt"): #url_text = pickle.load(open("url_text.txt","rb")) url_text = pickle.load(open("url_text.txt","rb")) print("read") else: for site in site_list: url_text = openURL(url_text, site) pickle.dump(url_text,open("url_text.txt","wb")) except Exception, e: print e tokens = tokenize(url_text) print "done tokenize" found = binarySearch("alpha",sorted(set(tokens))) print found ''' #create the tokens try: url_tokens = tokenize(url_text) url_bitokens = bigrams(url_tokens) url_tritokens = trigrams(url_tokens) fout = open("wikipedia_tokens.csv",'wb') for item in sorted(set(url_tokens)): if url_tokens.count(item) > 100: fout.write(item + "," + str(url_tokens.count(item)) + "\n") fout.close() fout = open("wikipedia_bitokens.csv",'wb') for item in sorted(set(url_bitokens)): if url_bitokens.count(item) > 80:
def test_case_1(self): self.assertEqual(binarySearch([1, 5, 23, 111], 111), 3)
def test_case_9(self): self.assertEqual( binarySearch([0, 1, 21, 33, 45, 45, 61, 71, 72, 73, 355], 354), -1)
def test_case_7(self): self.assertEqual( binarySearch([0, 1, 21, 33, 45, 45, 61, 71, 72, 73], 70), -1)
def test_case_4(self): self.assertEqual( binarySearch([0, 1, 21, 33, 45, 45, 61, 71, 72, 73], 33), 3)
def test_case_3(self): self.assertEqual(binarySearch([1, 5, 23, 111], 35), -1)