示例#1
0
 def test_no_replacement(self):
     """ensure that elements are sampled without replacement"""
     items = range(15)
     for _ in range(50):
         combination = mi.random_combination(items, len(items))
         self.assertEqual(len(combination), len(set(combination)))
     self.assertRaises(ValueError,
                       lambda: mi.random_combination(items,
                                                     len(items) + 1))
示例#2
0
 def test_no_replacement(self):
     """ensure that elements are sampled without replacement"""
     items = range(15)
     for _ in range(50):
         combination = mi.random_combination(items, len(items))
         self.assertEqual(len(combination), len(set(combination)))
     self.assertRaises(
         ValueError, lambda: mi.random_combination(items, len(items) + 1)
     )
示例#3
0
 def test_pseudorandomness(self):
     """ensure different subsets of the iterable get returned over many
     samplings of random combinations"""
     items = range(15)
     all_items = set()
     for _ in range(50):
         combination = mi.random_combination(items, 5)
         all_items |= set(combination)
     self.assertEqual(all_items, set(items))
示例#4
0
 def test_psuedorandomness(self):
     """ensure different subsets of the iterable get returned over many
     samplings of random combinations"""
     items = range(15)
     all_items = set()
     for _ in range(50):
         combination = mi.random_combination(items, 5)
         all_items |= set(combination)
     self.assertEqual(all_items, set(items))
示例#5
0
class TestSearch(unittest.TestCase):
    def test_search(self):
        data = [2, 3, 4, 5, 6, 7, 8, 12]
        self.assertEqual(binary_search(data, 5), 3)

    def test_searchChar(self):
        data = ['a', 'b', 'x', 'z']
        self.assertEqual(binary_search(data, 'x'), 2)


if __name__ == "__main__":
    unittest.main()

################BEST CASE########################
for i in range(0, 100):
    data = mit.random_combination(range(0, 100000 * (10 * (i + 1))),
                                  r=10000 * (10 * (i + 1)))
    mid = 10000 * (10 * (i + 1)) // 2
    start = time.time()
    print("Index of {}: {}".format(data[mid],
                                   binary_search_recursive(data, data[mid])))
    end = time.time()
    bs_wc.append(end - start)
    print(bs_bc[i])

#######################################

################WORST CASE########################
for i in range(0, 100):
    data = mit.random_combination(range(0, 100000 * (10 * (i + 1))),
                                  r=10000 * (10 * (i + 1)))
    start = time.time()