def test_randomized_select(self):
     for _ in range(1000):
         arr = random_int_array(100, 1000)
         print(arr)
         sorted_arr = sorted(arr)
         for i in range(len(arr)):
             self.assertEqual(sorted_arr[i], randomized_select(arr, i + 1))
Esempio n. 2
0
 def test_min_heapify(self):
     for i in range(1000):
         a1 = random_int_array(20, 100)
         print(a1)
         a2 = a1.copy()
         heapify(a1)
         print(a1)
         min_heapify(a2)
         print(a2)
         self.assertEqual(a1, a2)
Esempio n. 3
0
    def test_binary_search(self):
        arr = ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
        self.assertEqual(-1, binary_search(' ', arr))
        self.assertEqual(-1, binary_search('a', arr))
        self.assertEqual(-1, binary_search('ab', arr))
        self.assertEqual(-1, binary_search('bc', arr))
        self.assertEqual(-1, binary_search('cd', arr))
        self.assertEqual(-1, binary_search('de', arr))
        self.assertEqual(-1, binary_search('ef', arr))
        self.assertEqual(0, binary_search('aaa', arr))
        self.assertEqual(1, binary_search('bbb', arr))
        self.assertEqual(2, binary_search('ccc', arr))
        self.assertEqual(3, binary_search('ddd', arr))
        self.assertEqual(4, binary_search('eee', arr))

        # needle exists in haystack
        for _ in range(1000):
            arr1 = random_int_array(20, 1000)
            if arr1:
                arr1 = list(set(arr1))
                arr1.sort()
                needle = choice(arr1)
                print(arr1, needle)
                self.assertEqual(arr1.index(needle), binary_search(needle, arr1))

        # random needle value in haystack
        for _ in range(1000):
            arr1 = random_int_array(20, 1000)
            if arr1:
                arr1 = list(set(arr1))
                arr1.sort()
                needle = randrange(1000)
                print(arr1, needle)
                try:
                    index_val = arr1.index(needle)
                except ValueError:
                    index_val = -1
                print(binary_search(needle, arr1), index_val)
                self.assertEqual(index_val, binary_search(needle, arr1))
Esempio n. 4
0
    def test_something(self):
        self.assertEqual(True, True)

        self.assertEqual(linear_search(0, []), -1)

        a = [1, 2, 3, 4, 5]
        self.assertEqual(linear_search(0, a), -1)
        self.assertEqual(linear_search(1, a), 0)
        self.assertEqual(linear_search(2, a), 1)
        self.assertEqual(linear_search(3, a), 2)
        self.assertEqual(linear_search(4, a), 3)
        self.assertEqual(linear_search(5, a), 4)
        self.assertEqual(linear_search(6, a), -1)

        # needle found in haystack
        for _ in range(10000):
            a1 = []
            while not a1:
                a1 = random_int_array(100, 1000)
            n = a1[randrange(len(a1))]
            print(a1)
            print(n)
            self.assertEquals(linear_search(n, a1), a1.index(n))

        # random needle
        for _ in range(10000):
            a1 = []
            while not a1:
                a1 = random_int_array(100, 1000)
            n = randrange(1000)
            print(a1)
            print(n)
            try:
                ret = a1.index(n)
            except ValueError:
                ret = -1
            print(ret)
            self.assertEquals(linear_search(n, a1), ret)
Esempio n. 5
0
    def test_min_heap_push(self):
        for i in range(1000):
            r = random.randrange(100)
            print(r)

            a1 = random_int_array(20, 100)
            a2 = a1.copy()

            heappush(a1, r)
            print(a1)
            min_heap_push(a2, r)
            print(a2)

            self.assertEqual(a1, a2)
Esempio n. 6
0
 def test_sum_in_array(self):
     # the sum equals parameter passed to function
     for _ in range(1000):
         arr1 = random_int_array(20, 1000)
         if arr1:
             int1, int2 = choices(arr1, k=2)
             if int1 != int2:
                 print(arr1, int1, int2, int1 + int2)
                 l, r = sum_in_array(arr1, int1 + int2)
                 self.assertEqual(arr1[l] + arr1[r], int1 + int2)
     # the sum does no equal parameter passed to function
     for _ in range(1000):
         arr1 = random_int_array(20, 1000)
         sum_arr = []
         if arr1:
             for i in range(len(arr1)):
                 for j in range(i + 1, len(arr1)):
                     sum_arr.append(arr1[i] + arr1[j])
             x = randrange(-5000, 5000)
             while x in sum_arr:
                 x = randrange(-5000, 5000)
             print(sum_arr)
             print(arr1, x)
             self.assertEqual(sum_in_array(arr1, x), (-1, -1))
Esempio n. 7
0
    def test_min_heap_pop(self):
        for i in range(1000):
            a1 = random_int_array(20, 100)
            if not a1:
                continue
            heapify(a1)
            a2 = a1.copy()
            print('test array', a1)

            minkey1 = heappop(a1)
            print(minkey1, a1)
            minkey2 = min_heap_pop(a2)
            print(minkey2, a2)

            self.assertEqual(minkey1, minkey2)
            self.assertEqual(a1, a2)
Esempio n. 8
0
    def test_merge_sort(self):
        # self.assertEqual(merge_sort([]), [])

        # self.assertEqual(merge_sort([1]), [1])
        # self.assertEqual(merge_sort([1, 2]), [1, 2])
        # self.assertEqual(merge_sort([2, 1]), [1, 2])
        #
        # self.assertEqual(merge_sort([1, 2, 3]), [1, 2, 3])
        # self.assertEqual(merge_sort([1, 3, 2]), [1, 2, 3])
        # self.assertEqual(merge_sort([2, 1, 3]), [1, 2, 3])
        # self.assertEqual(merge_sort([2, 3, 1]), [1, 2, 3])
        # self.assertEqual(merge_sort([3, 1, 2]), [1, 2, 3])
        # self.assertEqual(merge_sort([3, 2, 1]), [1, 2, 3])
        #
        # self.assertEqual(merge_sort([1, 2, 3, 4]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([1, 2, 4, 3]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([1, 3, 2, 4]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([1, 3, 4, 2]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([1, 4, 2, 3]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([1, 4, 3, 2]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([2, 1, 3, 4]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([2, 1, 4, 3]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([2, 3, 1, 4]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([2, 3, 4, 1]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([2, 4, 1, 3]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([2, 4, 3, 1]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([3, 1, 2, 4]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([3, 1, 4, 2]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([3, 2, 1, 4]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([3, 2, 4, 1]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([3, 4, 1, 2]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([3, 4, 2, 1]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([4, 1, 2, 3]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([4, 1, 3, 2]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([4, 2, 1, 3]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([4, 2, 3, 1]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([4, 3, 1, 2]), [1, 2, 3, 4])
        # self.assertEqual(merge_sort([4, 3, 2, 1]), [1, 2, 3, 4])
        #
        # self.assertEqual(merge_sort([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 2, 3, 5, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 2, 4, 3, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 2, 4, 5, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 2, 5, 3, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 2, 5, 4, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 3, 2, 4, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 3, 2, 5, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 3, 4, 2, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 3, 4, 5, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 3, 5, 2, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 3, 5, 4, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 4, 2, 3, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 4, 2, 5, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 4, 3, 2, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 4, 3, 5, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 4, 5, 2, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 4, 5, 3, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 5, 2, 3, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 5, 2, 4, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 5, 3, 2, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 5, 3, 4, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 5, 4, 2, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([1, 5, 4, 3, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 1, 3, 4, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 1, 3, 5, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 1, 4, 3, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 1, 4, 5, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 1, 5, 3, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 1, 5, 4, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 3, 1, 4, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 3, 1, 5, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 3, 4, 1, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 3, 4, 5, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 3, 5, 1, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 3, 5, 4, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 4, 1, 3, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 4, 1, 5, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 4, 3, 1, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 4, 3, 5, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 4, 5, 1, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 4, 5, 3, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 5, 1, 3, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 5, 1, 4, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 5, 3, 1, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 5, 3, 4, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 5, 4, 1, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([2, 5, 4, 3, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 1, 2, 4, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 1, 2, 5, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 1, 4, 2, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 1, 4, 5, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 1, 5, 2, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 1, 5, 4, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 2, 1, 4, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 2, 1, 5, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 2, 4, 1, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 2, 4, 5, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 2, 5, 1, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 2, 5, 4, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 4, 1, 2, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 4, 1, 5, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 4, 2, 1, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 4, 2, 5, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 4, 5, 1, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 4, 5, 2, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 5, 1, 2, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 5, 1, 4, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 5, 2, 1, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 5, 2, 4, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 5, 4, 1, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([3, 5, 4, 2, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 1, 2, 3, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 1, 2, 5, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 1, 3, 2, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 1, 3, 5, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 1, 5, 2, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 1, 5, 3, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 2, 1, 3, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 2, 1, 5, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 2, 3, 1, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 2, 3, 5, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 2, 5, 1, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 2, 5, 3, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 3, 1, 2, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 3, 1, 5, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 3, 2, 1, 5]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 3, 2, 5, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 3, 5, 1, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 3, 5, 2, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 5, 1, 2, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 5, 1, 3, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 5, 2, 1, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 5, 2, 3, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 5, 3, 1, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([4, 5, 3, 2, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 1, 2, 3, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 1, 2, 4, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 1, 3, 2, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 1, 3, 4, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 1, 4, 2, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 1, 4, 3, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 2, 1, 3, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 2, 1, 4, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 2, 3, 1, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 2, 3, 4, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 2, 4, 1, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 2, 4, 3, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 3, 1, 2, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 3, 1, 4, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 3, 2, 1, 4]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 3, 2, 4, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 3, 4, 1, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 3, 4, 2, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 4, 1, 2, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 4, 1, 3, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 4, 2, 1, 3]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 4, 2, 3, 1]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 4, 3, 1, 2]), [1, 2, 3, 4, 5])
        # self.assertEqual(merge_sort([5, 4, 3, 2, 1]), [1, 2, 3, 4, 5])

        for i in range(1000):
            arr = random_int_array(100, 1000)
            print(arr)
            sorted_arr = sorted(arr)
            print(sorted_arr)
            merge_sort(arr)
            print(arr)
            self.assertEqual(sorted_arr, arr)
Esempio n. 9
0
    def test_something(self):
        self.assertEqual(length([]), 0)
        self.assertEqual(length([1]), 1)
        self.assertEqual(length([1, 2]), 2)
        self.assertEqual(length([1, 2, 3]), 3)
        for _ in range(1000):
            arr = random_int_array(20, 100)
            # print(arr)
            self.assertEqual(length(arr), len(arr))

        self.assertEqual(same([]), [])
        self.assertEqual(same([1]), [1])
        self.assertEqual(same([1, 2]), [1, 2])
        self.assertEqual(same([1, 2, 3]), [1, 2, 3])
        for _ in range(1000):
            arr = random_int_array(20, 100)
            # print(arr)
            self.assertEqual(same(arr), arr)

        self.assertEqual(reverse([]), [])
        self.assertEqual(reverse([1]), [1])
        self.assertEqual(reverse([1, 2]), [2, 1])
        self.assertEqual(reverse([1, 2, 3]), [3, 2, 1])
        for _ in range(1000):
            arr = random_int_array(20, 100)
            # print(arr)
            arr1 = list(arr)
            arr1.reverse()
            self.assertEqual(reverse(arr), arr1)

        self.assertEqual(nth_element([], 1), None)
        self.assertEqual(nth_element([1, 2, 3], -1), None)
        self.assertEqual(nth_element([1, 2, 3], 3), None)
        self.assertEqual(nth_element([1, 2, 3], 0), 1)
        self.assertEqual(nth_element([1, 2, 3], 1), 2)
        self.assertEqual(nth_element([1, 2, 3], 2), 3)
        for _ in range(1000):
            arr = random_int_array(20, 100)
            if arr:
                i = randrange(len(arr))
                # print(arr, i)
                self.assertEqual(nth_element(arr, i), arr[i])

        self.assertEqual(repeat('ha', -1), '')
        self.assertEqual(repeat('ha', 0), '')
        self.assertEqual(repeat('ha', 1), 'ha')
        self.assertEqual(repeat('ha', 2), 'haha')
        self.assertEqual(repeat('ha', 3), 'hahaha')
        for _ in range(1000):
            s = random_string(10)
            i = randrange(10)
            # print(s, i)
            self.assertEqual(repeat(s, i), s * i)

        self.assertEqual(pop_in_list([], 0), [])
        self.assertEqual(pop_in_list([1, 2, 3, 4, 5], 0), [2, 3, 4, 5])
        self.assertEqual(pop_in_list([1, 2, 3, 4, 5], 1), [1, 3, 4, 5])
        self.assertEqual(pop_in_list([1, 2, 3, 4, 5], 2), [1, 2, 4, 5])
        self.assertEqual(pop_in_list([1, 2, 3, 4, 5], 3), [1, 2, 3, 5])
        self.assertEqual(pop_in_list([1, 2, 3, 4, 5], 4), [1, 2, 3, 4])
        self.assertEqual(pop_in_list([1, 2, 3, 4, 5], 5), [1, 2, 3, 4, 5])
Esempio n. 10
0
    def test_quicksort(self):
        arr = []
        quicksort(arr)
        self.assertEqual([], arr)

        arr = [1]
        quicksort(arr)
        self.assertEqual([1], arr)

        arr = [1, 2]
        quicksort(arr)
        self.assertEqual([1, 2], arr)
        arr = [2, 1]
        quicksort(arr)
        self.assertEqual([1, 2], arr)

        arr = [1, 2, 3]
        quicksort(arr)
        self.assertEqual([1, 2, 3], arr)
        arr = [1, 3, 2]
        quicksort(arr)
        self.assertEqual([1, 2, 3], arr)
        arr = [2, 1, 3]
        quicksort(arr)
        self.assertEqual([1, 2, 3], arr)
        arr = [2, 3, 1]
        quicksort(arr)
        self.assertEqual([1, 2, 3], arr)
        arr = [3, 1, 2]
        quicksort(arr)
        self.assertEqual([1, 2, 3], arr)
        arr = [3, 2, 1]
        quicksort(arr)
        self.assertEqual([1, 2, 3], arr)

        arr = [1, 2, 3, 4]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [1, 2, 4, 3]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [1, 3, 2, 4]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [1, 3, 4, 2]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [1, 4, 2, 3]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [1, 4, 3, 2]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [2, 1, 3, 4]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [2, 1, 4, 3]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [2, 3, 1, 4]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [2, 3, 4, 1]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [2, 4, 1, 3]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [2, 4, 3, 1]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [3, 1, 2, 4]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [3, 1, 4, 2]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [3, 2, 1, 4]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [3, 2, 4, 1]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [3, 4, 1, 2]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [3, 4, 2, 1]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [4, 1, 2, 3]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [4, 1, 3, 2]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [4, 2, 1, 3]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [4, 2, 3, 1]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [4, 3, 1, 2]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)
        arr = [4, 3, 2, 1]
        quicksort(arr)
        self.assertEqual([1, 2, 3, 4], arr)

        for i in range(5000):
            arr = random_int_array(100, 1000)
            print(arr)
            quicksort(arr)
            self.assertEqual(sorted(arr), arr)