def test_empty_array(self): arr = numpy.empty(0) quicksort(arr, 0, len(arr) - 1) try: numpy.testing.assert_array_equal(arr, []) self.assertTrue(True) except AssertionError as error: self.assertTrue(False, error)
def test_one_element_array(self): arr = numpy.array([2]) quicksort(arr, 0, len(arr) - 1) try: numpy.testing.assert_array_equal(arr, numpy.array([2])) self.assertTrue(True) except AssertionError as error: self.assertTrue(False, error)
def test_small_array(self): arr = numpy.array([4, 8, 3, 9, 0, 8]) quicksort(arr, 0, len(arr) - 1) try: numpy.testing.assert_array_equal(arr, numpy.array([0, 3, 4, 8, 8, 9])) self.assertTrue(True) except AssertionError as error: self.assertTrue(False, error)
num = random.randint(low, high) used.add(num) for j in range(3): index = random.randint(0, length - 1) arr.insert(index, num) length += 1 num = random.randint(low, high) while num in used: num = random.randint(low, high) index = random.randint(0, length - 1) arr.insert(index, num) return arr def non_duplicate(arr): i = 0 while True: if i == len(arr) - 1: return arr[i] elif arr[i] == arr[i + 1]: i += 3 else: return arr[i] # arg of generate() is length of arr where length = 3x + 1 and x is the # number of repeated integers test = generate(7) print(test) quicksort(test, 0, len(test) - 1) print(non_duplicate(test))
from Heapsort import heapsort arr1 = [] for i in range(9999): arr1.append(random.randint(-99, 99)) arr2 = arr1.copy() arr3 = arr1.copy() arr4 = arr1.copy() arr5 = arr1.copy() arr6 = arr1.copy() arr7 = arr1.copy() arr8 = arr1.copy() start = time.time() quicksort(arr1, 0, len(arr1) - 1) print("Quick Sort time: %ss" % (time.time() - start)) start = time.time() shellsort(arr2) print("Shell Sort time: %ss" % (time.time() - start)) start = time.time() mergesort(arr3) print("Merge Sort time: %ss" % (time.time() - start)) start = time.time() heapsort(arr4) print("Heap Sort time: %ss" % (time.time() - start)) start = time.time()
if count == length: print_itinery(visited) return else: dfs(graph, vertex, visited, count) def print_itinery(flights): global valid valid = True itinery = [] for i in range(len(flights)): itinery.append(flights[i][0]) if i == len(flights) - 1: itinery.append(flights[i][1]) print(itinery) def compute_itinery(graph, start): dfs(graph, start) if valid == False: print('Invalid inventory') arr = [('SFO', 'COM'), ('COM', 'YYZ')] length = len(arr) quicksort(arr, 0, length - 1) graph = create_graph(arr) start = 'SFO' valid = False compute_itinery(graph, start)