Beispiel #1
0
class BinarySearchTest(TestCase):
    """Get the index of the item with an expected number of loops in\
     array [1, 2 . . . 20]
       Returns a dictionary containing {count: value, index: value}
    """
    def setUp(self):
        self.one_to_twenty = BinarySearch(20, 1)
        self.two_to_forty = BinarySearch(20, 2)
        self.ten_to_thousand = BinarySearch(100, 10)

    def test_small_list_search(self):
        search = self.one_to_twenty.search(16)
        self.assertGreater(5,
                           search['count'],
                           msg='should return {count: 4, index: 15} for 16')
        self.assertEqual(15,
                         search['index'],
                         msg='should return {count: 4, index: 15} for 16')

    def test_medium_list_search(self):
        search1 = self.two_to_forty.search(16)
        search2 = self.two_to_forty.search(40)
        search3 = self.two_to_forty.search(33)
        self.assertGreater(5,
                           search1['count'],
                           msg='should return {count: 4, index: 7} for 16')
        self.assertEqual(7,
                         search1['index'],
                         msg='should return {count: 4, index: 7} for 16')
        self.assertEqual(0,
                         search2['count'],
                         msg='should return {count: 0, index: 19} for 40')
        self.assertEqual(19,
                         search2['index'],
                         msg='should return {count: 5, index: 19} for 40')

        self.assertGreater(4,
                           search3['count'],
                           msg='should return {count: 3, index: -1} for 33')
        self.assertEqual(-1,
                         search3['index'],
                         msg='should return {count: 3, index: -1} for 33')

    def test_large_list_search(self):
        search1 = self.ten_to_thousand.search(40)
        search2 = self.ten_to_thousand.search(880)
        search3 = self.ten_to_thousand.search(10000)
        self.assertGreater(
            7,
            search1['count'],
            msg='should return {count: # <= 7, index: 3} for 40')
        self.assertEqual(3,
                         search1['index'],
                         msg='should return {count: # <= 7, index: 3} for 40')
        self.assertGreater(
            4,
            search2['count'],
            msg='should return {count: # <= 3, index: 87} for 880')
        self.assertEqual(
            87,
            search2['index'],
            msg='should return {count: # <= 3, index: 87} for 880')

        self.assertGreater(7,
                           search3['count'],
                           msg='should return {count: 3, index: -1} for 10000')
        self.assertEqual(-1,
                         search3['index'],
                         msg='should return {count: 3, index: -1} for 10000')
Beispiel #2
0
 def setUp(self):
     self.one_to_twenty = BinarySearch(20, 1)
     self.two_to_forty = BinarySearch(20, 2)
     self.ten_to_thousand = BinarySearch(100, 10)
names_1 = f.read().split("\n")  # List containing 10000 names
f.close()

f = open('names_2.txt', 'r')
names_2 = f.read().split("\n")  # List containing 10000 names
f.close()

duplicates = []  # Return the list of duplicates in this data structure

# Replace the nested for loops below with your improvements
# for name_1 in names_1:
#     for name_2 in names_2:
#         if name_1 == name_2:
#             duplicates.append(name_1)

tree_name = BinarySearch(names_1[0])

for name in range(1, len(names_1)):
    tree_name.insert(names_1[name])
for names_2 in names_2:
    if (tree_name.contains(names_2)):
        duplicates.append(names_2)

end_time = time.time()
print(f"{len(duplicates)} duplicates:\n\n{', '.join(duplicates)}\n\n")
print(f"runtime: {end_time - start_time} seconds")

# ---------- Stretch Goal -----------
# Python has built-in tools that allow for a very efficient approach to this problem
# What's the best time you can accomplish?  Thare are no restrictions on techniques or data
# structures, but you may not import any additional libraries that you did not write yourself.